[英]Javascript chaining promise
我需要返回一個包含 object 的 promise。 我已經嘗試過了,但我不確定這是否是正確的方法?
export const getCurrentLocation = async () => {
const currentLoc = {
code: null,
position: null,
watchId: null,
};
const promise = new Promise((resolve, reject) => {
navigator.geolocation.getCurrentPosition(
(position) => {
currentLoc.code = 200;
currentLoc.position = position;
resolve(currentLoc);
},
(error) => {
console.log('Postion==err===>', error);
currentLoc.code = 400;
reject(currentLoc);
},
{enableHighAccuracy: true, timeout: 20000, maximumAge: 1000},
);
}).then(
new Promise((resolve, reject) => {
const watchId = navigator.geolocation.watchPosition();
currentLoc.watchId = watchId;
resolve(currentLoc);
}),
);
return promise;
};
在 Promise 塊內我有異步 function
您可以使用async/await :
const obj = {}; const populate = async() => { await (() => obj.value1 = 'Deepak')(); await (() => obj.value2 = 'JavaScript')(); console.log(obj); }; populate();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.