如何对angularjs中的多个函数做出承诺

时间:2017-08-02 13:10:41

标签: javascript angularjs angular-promise

代码说明:

如果互联网可用,它将根据不同的方法同步来自不同表的未同步数据。但是,我想知道如何为这些不同的函数添加承诺。我用来使所有函数启动的逻辑同时但是,我想基于function1的成功启动function2并重复相同的过程,任何人都可以告诉我该怎么做。

function syncApp () {
  $log.log('offlineOnlineSync got called');

  Contact.syncApp().then(function (resp) {
    $log.log('Contact sync got called', resp);

    WorkerGroup.syncApp().then(function (resp) {
      $log.log('WorkerGroup sync got called', resp);

      Category.syncApp().then(function (resp) {
        $log.log('Category sync got called', resp);

        Vehicle.syncApp().then(function (resp) {
          $log.log('Vehicle sync got called', resp);

          Daybook.syncApp().then(function (resp) {
            $log.log('Daybook sync got called', resp);
          }, CommonService.errorHandler);

        }, CommonService.errorHandler);

      }, CommonService.errorHandler);

    }, CommonService.errorHandler);

  }, CommonService.errorHandler);
}

在第一种方法中,发生的过程是这样的,但在完成上述过程之前,第二种方法被调用。

    prom = DB.update('contacts', servResp, 'id', key)
      .then(function () {
        if (servResp.type === 'Worker') {
            WorkerGroup.checkGroupForTempIds(key, servResp.id)
             .then(function (resp) {
                  $log.log('Response in contact service', resp);
            }, function (err) {
                  $log.log('err: ', err);
            });
        } // fix me // not needed
    }, function (err) {
          $log.log('err: ', err);
    });

    $log.log('serverresponseid', servResp.id);
    $log.log('key', key);
    var daybook_updatequery = 'UPDATE daybook SET contact_id = ? WHERE contact_id = ?';

    $cordovaSQLite.execute(Database.db, daybook_updatequery, [servResp.id, key])
      .then(function (resp) {
        $log.log('response', resp);
        defer.resolve('success');
    }, function (err) {
        q.reject(err);
        $log.log(err);
    });

    proms.push(prom);
});
$q.all(proms).then(function () {
    defer.resolve('success');
});

2 个答案:

答案 0 :(得分:2)

您可以链接承诺,因此您只需要一个错误处理程序:

function syncApp () {
  $log.log('offlineOnlineSync got called');
  return Contact.syncApp().then(function (resp) {
    $log.log('Contact sync got called', resp);
    return WorkerGroup.syncApp();
  }).then(function (resp) {
    $log.log('WorkerGroup sync got called', resp);
    return Category.syncApp();
  }).then(function (resp) {
    $log.log('Category sync got called', resp);
    return Vehicle.syncApp();
  }).then(function (resp) {
    $log.log('Vehicle sync got called', resp);
    return Daybook.syncApp();
  }).then(function (resp) {
    $log.log('Daybook sync got called', resp);
  }).catch(function(err) {
    CommonService.errorHandler(err);
  });
}

答案 1 :(得分:1)

要让父承诺等待链式承诺的完成,return对父.then处理函数的链式承诺非常重要:

function getProm(servResp, key) {
    ͟r͟e͟t͟u͟r͟n͟ DB.update('contacts', servResp, 'id', key)
      .then(function () {
        if (servResp.type === 'Worker') {
            ͟r͟e͟t͟u͟r͟n͟ WorkerGroup.checkGroupForTempIds(key, servResp.id)
             .then(function (resp) {
                  $log.log('Response in contact service', resp);
                  ͟r͟e͟t͟u͟r͟n͟  resp;
            }, function (err) {
                  $log.log('err: ', err);
                  //IMPORTANT
                  throw err;
            });
        } else {
            ͟r͟e͟t͟u͟r͟n͟ "something";
        };
    }, function (err) {
          $log.log('err: ', err);
          //IMPORTANT
          throw err;
    });
}

另外,为了避免转换拒绝承诺履行承诺,在拒绝处理函数中使用throw statement非常重要。

.then方法处理函数省略return statement时,该方法返回一个解析undefined的新promise,该promise不等待在该处理函数中启动的任何异步操作。

有关详细信息,请参阅You're Missing the Point of Promises