前提・実現したいこと
設定パターン1~2のコードがありますが、1はコールバック、2はプロミスで書いています。この場合どちらで書くべきでしょうか。
コールバックでも問題ないのか、それともプロミスで書いたほうが良いのか教えて頂ければ幸いです。
該当のソースコード
javascript
1//設定 パターン1 2const ChangePermission = (callback) => { 3 if('permissions' in navigator) { 4 navigator.permissions.query({name:'notifications'}).then((notificationPerm) => { 5 notificationPerm.onchange = () => { 6 if(notificationPerm.state === "granted"){ 7 console.debug('通知が許可に変更'); 8 callback(); 9 } 10 }; 11 }); 12 } 13};
javascript
1//設定 パターン2 2const ChangePermission = () => { 3 return new Promise((resolve, reject) => { 4 if('permissions' in navigator) { 5 navigator.permissions.query({name:'notifications'}).then((notificationPerm) => { 6 notificationPerm.onchange = () => { 7 if(notificationPerm.state === "granted"){ 8 console.debug('通知が許可に変更'); 9 resolve(); 10 } 11 }; 12 }); 13 } 14 }); 15};
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/03/16 03:19