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条回答 默认 最新

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥15 有偿找一份verilog语言的cnn代码带讲解
      • ¥15 关于#mysql#的问题:mysql 组内排名,取各组的销售金额前10
      • ¥15 有人用过颜色传感器吗?
      • ¥50 求一个SQL长料切短料的优化排版算法
      • ¥15 python 删除TXT文档中小写字母
      • ¥15 ValueError: not enough values to unpack (expected 2, got 0)
      • ¥15 js怎么禁止修改域名
      • ¥15 Cursor为什么在Windows11打不开
      • ¥30 java调用javacv遇到的问题
      • ¥15 如何使用matlab画出带宽100Khz,时长100ms信号的LFM形式的时频图啊