如何对 angularjs 中的多个函数做出承诺
How to have promise for the multiple functions in angularjs
代码解释:
如果互联网可用,它将根据不同的 methods.But 同步来自不同表的未同步数据,我想知道如何为这些不同的 functions.The 逻辑添加承诺,我在使所有功能同时启动但是,我想根据 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');
});
您可以链接承诺,因此您只需要一个错误处理程序:
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);
});
}
要让父级承诺等待链式承诺的完成,重要的是 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 时,方法 returns 一个解析 undefined
的新承诺,并且该承诺不等待任何异步操作在该处理程序函数中启动。
有关详细信息,请参阅 You're Missing the Point of Promises
代码解释:
如果互联网可用,它将根据不同的 methods.But 同步来自不同表的未同步数据,我想知道如何为这些不同的 functions.The 逻辑添加承诺,我在使所有功能同时启动但是,我想根据 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');
});
您可以链接承诺,因此您只需要一个错误处理程序:
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);
});
}
要让父级承诺等待链式承诺的完成,重要的是 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 时,方法 returns 一个解析 undefined
的新承诺,并且该承诺不等待任何异步操作在该处理程序函数中启动。
有关详细信息,请参阅 You're Missing the Point of Promises