【问题标题】:Changing my callbacks, promise to async await更改我的回调,承诺异步等待
【发布时间】:2019-05-05 13:12:49
【问题描述】:

我的代码:

回调:

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']

异步等待:

如何使用 async await 转换上述代码?

对于 javascript 应用程序来说,哪个是好的流控制?

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

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

【问题讨论】:

  • 你的 firstthird 事物/函数不应该与承诺有任何关系,它们没有任何异步。
  • 如果我不为第一个函数编写新的 Promise,那么 .then() 将如何工作?
  • 不会:不需要。你只需写first(); second().then(third);first(); await second(); third();

标签: javascript callback promise async-await


【解决方案1】:

如何使用 async await 转换上述代码?

async/await 不是 Promise 的替代品,它是 then()catch() 的替代品。你仍然会使用 Promise。因此,您可以从 Promises 部分中获取 firstsecondthird 定义,然后:

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 应用程序来说,哪个是好的流控制?

客观地说,没有一个更好;但是async/await 是最易读的,回调是最明确的(then 代码在中间)。

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

Promise 通常可以完成那些库所做的所有事情,并且还被选中进行标准化。除非您维护需要它们的旧代码,否则您可能会忘记这些库。

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

它似乎在做你想让它做的事,或多或少,没有明显的效率或易读性问题。我会说没关系。

【讨论】:

    【解决方案2】:

    如何使用 async await 转换上述代码?

    使用async/await 是另一种使用Promises 的方式。这是最好的视觉效果,因为您不会迷失在then/catch 中,但您可以使用它。这里有两个例子。第一个有then,第二个没有。

    const first = async (value) => {
      return new Promise((resolve, reject) => {
        resolve('first ');
      });
    };
    const second = async (value) => {
      return new Promise((resolve, reject) => {
        resolve(value + 'second ');
      });
    };
    const third = async (value) => {
      return new Promise((resolve, reject) => {
        resolve(value + 'third');
      });
    };
    
    
    first('').then(second).then(third).then( value => console.log(value) );

    const first = async () => {
      return new Promise((resolve, reject) => {
        resolve('first ');
      });
    };
    const second = async () => {
      return new Promise((resolve, reject) => {
        resolve('second ');
      });
    };
    const third = () => {
      return 'third';
    
    };
    
    async function main() {
      
      let firstValue = await first();
      
      let secondValue = await second();
      
      let thirdValue = third();
      
      return  firstValue + ' ' + secondValue + ' ' + thirdValue;
    }
    
    main()
      .then(console.log)
      .catch(err => { console.log('Error:', err) });

    对于 javascript 应用程序来说,哪个是好的流控制?

    通常当您需要在函数中间使用 Promise 或函数的多个异步调用时。

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

    我不知道有什么库可以使用 async/await。对不起!

    【讨论】:

      猜你喜欢
      • 2018-03-02
      • 2017-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-20
      • 2018-02-03
      • 2018-03-05
      相关资源
      最近更新 更多