代码之家  ›  专栏  ›  技术社区  ›  Cheryl Blossom

将价值带入承诺链[重复]

  •  1
  • Cheryl Blossom  · 技术社区  · 5 年前

    所以,我有一个代码,我试图连续运行承诺,并携带变量,如果它遇到错误,它将被捕获。

    var x = new Promise(function(resolve, reject) {
    var one = 1
        resolve(one)
        // if error happens go to catch
    })
    .then(function(value) {
    var two = 2;
        resolve(two);
        // if error happens go to catch
    })
    .then(function(value) {
    var three = 3;
        resolve(three);
        // if error happens go to catch
    })
    .then(function(value) {
    console.log(one + two + three);
    })
    .catch(function(value) {
        // reverse all the execution
    })
    
    x;
    

    我正在创造一个承诺

    第一个承诺,用于循环包含要插入的某些对象

    第二个承诺将在我的第一个表中插入一些行

    第四个承诺会完成一些事情,等等

    以及一个捕获,它将删除由错误插入和剪切的数据。

    顺便说一句,我正在使用mysql。对于能够帮助我的人,请,我需要你。谢谢

    如果您对我正在转换的真实代码感到好奇,这就是我目前所做的: https://pastebin.com/Ui5vcUDg

    2 回复  |  直到 4 年前
        1
  •  1
  •   nicholaswmin    5 年前

    下面介绍如何通过创建累加器对象向下传递值,然后在每个步骤中添加result属性,从而将值向下传递承诺链:

    // Promises
    
    var one = new Promise((resolve, reject) => {
      resolve(1)
    })
    
    var two = new Promise((resolve, reject) => {
      resolve(2)
    })
    
    var three = new Promise((resolve, reject) => {
      resolve(3)
    })
    
    // Promise Chain
    
    one.then(one => {
      return ({ one })
    })
    .then(acc => {
      return two.then(two => ({ ...acc, two }))
    })
    .then(acc => {
      return three.then(three => ({ ...acc, three }))
    })
    .then(result => {
      console.log(result)
    })
    .catch(err => {
      console.error('one, two or three failed', err)
    })

    catch

    // Promises
    
    var one = new Promise((resolve, reject) => {
      reject(new Error('Promise one error :('))
    })
    
    var two = new Promise((resolve, reject) => {
      resolve(2)
    })
    
    // Promise Chain
    
    one.then(one => {
      return ({ one })
    })
    .then(acc => {
      return two.then(two => ({ ...acc, two }))
    })
    .then(result => {
      console.log(result)
    })
    .catch(err => {
      console.error(err)
    })

    throw 在一个小时内 then :

    var one = new Promise((resolve, reject) => {
      resolve(1)
    })
    
    var two = new Promise((resolve, reject) => {
      resolve(2)
    })
    
    one.then(one => {
      return ({ one })
    })
    .then(acc => {
      throw new Error('Error by explicit throw')
    })
    .then(acc => {
      return two.then(two => ({ ...acc, two }))
    })
    .then(result => {
      console.log(result)
    })
    .catch(err => {
      console.error(err)
    })

    two.then(two => ({ ...acc, two }))
    

    大致相当于:

    two.then(two => {
      acc.two = two
    
      return acc
    })
    

    以防此语法混淆。

        2
  •  0
  •   Vladyslav Usenko    5 年前

    在放入“then”的回调中,不需要调用resolve()来调用链的下一步。您只需要返回一些值,链的下一步将接收它作为参数。

    您可能混淆了承诺的创建和链接方式。创建承诺时,向其构造函数提供回调,其参数为“resolve”和“reject”,它们也是回调。它们预期将由promise构造函数中回调内的代码调用。调用resolve时,承诺将使用您提供的值进行解析。拒绝也一样。