更改我的回调,承诺异步等待 [英] Changing my callbacks, promise to async await

查看:98
本文介绍了更改我的回调,承诺异步等待的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码:

回调:

const first = () => {
  console.log('first');
};
const second = (callback) => {
   setTimeout(() => {
    console.log('second');
    callback();
  }, 2000);
};
const third = () => {
   console.log('third');
};

first();
second(third);   OUTPUT: 'first', 'second', 'third'

承诺:

const first = () => new Promise((resolve, reject) => {
  resolve('first');
});
const second = () => new Promise((resolve, reject) => {
  setTimeout(() => {
    resolve('second');
  }, 2000);
});
const third = () => {
  console.log('third');
};

first()
 .then((firstPromiseValue) => {
   console.log(firstPromiseValue);
   second()
    .then((secondPromiseValue) => {
      console.log(secondPromiseValue);
      third();
    })
 });   OUTPUT: 'first', 'second', 'third'

全部承诺:

const first = new Promise((resolve, reject) => {
  resolve('first');
});
const second = new Promise((resolve, reject) => {
  setTimeout(() => {
    resolve('second');
  }, 2000);
});
const third = new Promise(function(resolve, reject) {
  resolve('third');
});
Promise.all([first, second, third]).then((values) => {
  console.log(values);
}); OUTPUT: ['first', 'second', 'third']

异步等待:

如何使用异步等待转换上述代码?

How to convert this above code using async await?

哪个是针对javascript应用程序的良好流控制?

Which is the good flow control for javascript applications?

一些使用async.waterfall等方法的异步库呢?

What about some async library which uses methods like async.waterfall, etc..

顺便问一下,我上面的代码可以吗?

By the way, is my above code is ok or not?

推荐答案

如何使用异步等待转换上述代码?

How to convert this above code using async await?

async/await不是promise的替代品,它是then()catch()的替代品.您仍然会使用诺言.因此,您将从承诺"部分中选择firstsecondthird定义,然后:

async/await is not a replacement for promises, it is a replacement for then() and catch(). You'd still be using promises. So you'd take the first, second and third definition from your Promises section, and then:

async function firstSecondThird() {
  let firstPromiseValue = await first();
  console.log(firstPromiseValue);
  let secondPromiseValue = await second();
  console.log(secondPromiseValue);
  third(); // not a promise, no need to await
}
firstSecondThird();

哪个是针对javascript应用程序的良好流控制?

Which is the good flow control for javascript applications?

客观地讲,没有一个是更好的.但是async/await是最易读的,回调是最明确的(then代码位于中间).

Objectively, none of them are better; but async/await is the most readable, callbacks the most explicit (with then code being in the middle).

一些使用async.waterfall等方法的异步库呢?

What about some async library which uses methods like async.waterfall, etc..

Promise通常会执行这些库所做的所有事情,并且也会被选择进行标准化.除非您维护需要它们的旧代码,否则您可能会忘记这些库.

Promises generally do everything those libraries do, and also got selected to be standardised. You may forget about those libraries unless you are maintaining old code that requires them.

顺便问一下,我上面的代码可以吗?

By the way, is my above code is ok or not?

它似乎或多或少地完成了您想要的操作,而效率或易读性方面没有明显的问题.我会没事的.

It seems to do what you want it to do, more or less, without obvious problems in efficiency or legibility. I'd say it's OK.

这篇关于更改我的回调,承诺异步等待的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆