Promise
then 等 resolve,等到 resolve 就执行 then ,否则执行 catch
用 promise 的好处:
避免多次判断
job1.then(job2).then(job3).catch(handleError);
job1成功就执行job2成功再job3,其中任何一个失败就执行catch
setTimeout(callback, 1000); // 1秒钟后调用callback函数
setTimeout的作用:防止过渡消耗CPU
关于如何取得promise返回的**[[PromiseValue]]**中的值:
只能在内部获取:
const getUserInfo = function(){
return new Promise(function (resolve, reject) {
wx.getStorage({
key: "userInfo",
success: function (res) {
let userInfo = res.data;
resolve(userInfo);
}
});
});
}
module.exports = {
getUserInfo: getUserInfo,
}
userInfo.getUserInfo().then(function(res){
// 只能在这里面输出
console.log(res)
});
// 不知道怎么把得到的数据放到当前的环境中,本人不太了解promise,知道的大佬请指教!