weixin_33738578 2019-10-10 11:20 采纳率: 0%
浏览 96

Javascript promise.all()

I have this piece of code.

function a() {
  var promise1 = Promise.resolve(3);
  var promise2 = 42;
  var promise3 = new Promise(function(resolve, reject) {
    setTimeout(resolve, 2000, 'foo');
  });

  Promise.all([promise1, promise2, promise3]).then(function(values) {
    console.log("done", values);
  });
}

async function b() {
 await a(); 
}

b();
console.log("here")

Here, we get the output

"here"

and then after two seconds, we get

"done" Array [3, 42, "foo"]

How do I change this code so that inside function b(), we are actually waiting for a() to complete and then continue the execution of the code?

Hence, the output I want is

Wait two seconds and see

"done" Array [3, 42, "foo"]

"here"

  • 写回答

4条回答 默认 最新

  • weixin_33717117 2019-10-10 11:29
    关注

    You can write the above code like this:

    function a() {
        var promise1 = Promise.resolve(3);
        var promise2 = 42;
        var promise3 = new Promise(function (resolve, reject) {
            setTimeout(resolve, 2000, 'foo');
        });
    
        // Promise.all([promise1, promise2, promise3]).then(function (values) {
        //     console.log("done", values);
        // });
    
        return Promise.all([promise1, promise2, promise3]);
    }
    
    async function b() {
        let values = await a();
        console.log('done', values);
        // return values; // This will get automatically get wrapped into a promise.
        return Promise.resolve(values);
    }
    
    b().then(() => { console.log("here") });
    

    Here a returns a promise and after that b also returns a promise which is immediately resolved.

    评论
  • weixin_33743880 2019-10-10 11:30
    关注

    You can do it in more than one way.

    Form 1

    First, because a doesn't return, as stated by @chris-p-bacon, instead of handling the Promise itself, you can return it.

    Instead of

    Promise.all([promise1, promise2, promise3]).then(function(values) {
      console.log("done", values);
    });
    

    use

    return Promise.all([promise1, promise2, promise3]);
    

    Form 2

    The other way is to make the a function an async function, and then await for the Promise.all, this way:

    async function a() {
      ...
    
      await Promise.all([promise1, promise2, promise3]);
    }
    

    In this second form, different than the first form, you can still handle the Promise after it returns:

    async function a() {
      ...
    
      var values = await Promise.all([promise1, promise2, promise3]);
      console.log("done", values);
    }
    

    Conclusion

    Both forms answer your request to change your piece of code, and it will be almost equivalent to your code. But notice that if you were using the catch function you would have to use a try-catch instead, around the await.

    评论
  • weixin_33693070 2019-10-10 11:31
    关注

    b is the async function. So either you can put await front while calling it then execute rest or do the .then like

    b().then(res => {
     //rest codes  
     console.log("here")
    })
    
    

    By the way that is not required to put await in front of promise2 as thats not even a promise object.

    评论
  • weixin_33691817 2019-10-10 11:37
    关注

    Your promise always runs asynchronously so that you have to wait until it get resolved and then you can print your "here" console.

    function a() {
      var promise1 = Promise.resolve(3);
      var promise2 = 42;
      var promise3 = new Promise(function(resolve, reject) {
        setTimeout(resolve, 2000, 'foo');
      });
    
      return Promise.all([promise1, promise2, promise3]).then(function(values) {
       return values;
      });
    }
    
     function b() {
     a().then( function(res) {
      console.log("done", res);
      console.log("here")
     })
    }
    
    b();

    </div>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于非编程寻迹小车的分析
  • ¥15 java导出EXCEL这里已经执行成功了,但是流浪器没有下载
  • ¥15 帮我把代码改的能正常运行就行
  • ¥50 有限元修正、模型修正、最小二乘法,详细步骤
  • ¥15 用plc编写梯形程序
  • ¥15 关于#物联网#的问题:新大陆AIOT中,按照手册教程进行添加,设置完成后网关一直不上线,显示Never这是网络服务的信息
  • ¥15 这个要用一维热方程但是我不知道怎么运用这个公式
  • ¥15 OpenFOAM多孔介质传热模型建模
  • ¥15 QT 实现 RSTP 语音对讲功能
  • ¥15 用C语言写的一个程序遇到了两个问题第一是偏移正确但读取不到坐标,第二个问题是自己定义的函数实现不了获取指定进程模块。