gpt4 book ai didi

javascript - 使用 Bluebird promise

转载 作者:行者123 更新时间:2023-11-29 21:55:28 26 4
gpt4 key购买 nike

我有 myPeople 函数,它在其中调用这样的 promise 函数

var myPeople = function(){
var go;
return new Promise (function(resolve){
User
.getPeople()
.then(function(allPeople){
go = allPeople;
//console.log(go)
resolve(go);
})
})
return go;
}

如果我在 block 内记录我的 go 我得到了我的对象,但我无法让它返回这个对象..

最佳答案

Chain the promise,同时避免 then(success, fail) 反模式:

var myPeople = function(){
return User.getPeople()
.then(function(allPeople){ //
console.log(allPeople);
return allPeople.doSomething(); // filter or do whatever you need in
// order to get myPeople out of
// allPeople and return it

});
});
}

然后在外面:

myPeople.then(function(people){
console.log(people); // this will log myPeople, which you returned in the `then`
});

关于javascript - 使用 Bluebird promise ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26638185/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com