V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
photon006
V2EX  ›  Node.js

promise 批量 settle 执行结果如何使用参数

  •  
  •   photon006 · 2016-04-05 10:35:16 +08:00 · 3181 次点击
    这是一个创建于 2915 天前的主题,其中的信息可能已经有所发展或是发生改变。

    node.js 有一个批量异步处理,在 bluebird settle 执行完还需要使用 id 咋办,请问如何获取?

    var queries = array.map(function(id){
        return User.schema.find({user_id: id}).execAsync();
    });
    return Promise.settle(queries).then(function(results){
        results.forEach(function(result){
            if(result.isFulfilled()){
                //这里如何使用 map 遍历时的 id ?
                
            }else if(result.isRejected()){
                error(result.reason());
            }
        });
    });
    

    settle 是异步执行在外面定义一个变量临时存储 id 好像不行吧,如果是闭包倒是可以在每个异步执行完获取 id ,因为在同一个 function 作用域。

    2 条回复    2016-04-05 12:49:29 +08:00
    zhuangzhuang1988
        1
    zhuangzhuang1988  
       2016-04-05 11:15:43 +08:00   ❤️ 1
    var queries = array.map(function(id){
    return User.schema.find({user_id: id}).execAsync();
    });
    ==>
    var queries = array.map(function(id){
    return User.schema.find({user_id: id}).execAsync().then(data => {
    return {id: id, data: data}
    });
    });
    这样就好了...
    photon006
        2
    photon006  
    OP
       2016-04-05 12:49:29 +08:00
    @zhuangzhuang1988 Got it , thanks 。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5312 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 08:28 · PVG 16:28 · LAX 01:28 · JFK 04:28
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.