代码之家  ›  专栏  ›  技术社区  ›  Sameer

更改我的回调,承诺异步等待

  •  -2
  • Sameer  · 技术社区  · 7 年前

    我的代码:

    回调:

    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.瀑布等方法的异步库呢。。

    顺便问一下,我上面的代码是否正确?

    2 回复  |  直到 7 年前
        1
  •  4
  •   Amadan    7 年前

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

    async/await 不是承诺的替代品,而是承诺的替代品 then() catch() . 你仍然会使用承诺。那你就拿这个 first , second third “承诺”部分中的定义,然后:

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

    客观上,没有一个更好;但是 异步/等待 是最可读的,回调是最显式的(带有 then 代码位于中间)。

    那么使用async.瀑布等方法的异步库呢。。

    顺便问一下,我上面的代码是否正确?

        2
  •  -1
  •   Javier García    7 年前

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

    使用 async/await 是使用承诺的另一种方式。这是最美好的视觉,因为你不会迷失在 then/catch

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

    通常当你需要在函数的中间使用承诺或者多个异步调用函数时。

    我不知道有什么库可以使用async/await。很抱歉