1

I am working On NodeJS Project , I used Promise In my code to chain some methods , I needed to abort in one of the 'thens' chain

findEmployeeByCW('11111', "18-09-2016").
then(function () {
    return findEmployeeByCWE('111111', "18-09-2016", '111111')
}, function () {
    console.log('createEmployeeLoginBy')
    createEmployeeLoginBy('111111', "18-09-2016", '111111').
    then(function (log) {
        SaveEmployeeLogToDb(log)
        // ***************
        // ^_^ I need to exit here ....
    })
})
.then(function (log) {
    return updateLoginTimeTo(log, '08-8668', '230993334')
}, function () {
    return createNewEmployeeLog('224314', "18-09-2016",
        '230993334', '08-99')
})
.then(SaveEmployeeLogToDb).then(DisplayLog).catch(function (e) {
    console.log(e);
})
Morteza Tourani
  • 3,506
  • 5
  • 41
  • 48

2 Answers2

2

If I understand the intention correctly, there is no need here to cancel or throw.

You should be able to achieve your ends by rearrangement :

findEmployeeByCW('11111', "18-09-2016")
.then(function() {
    return findEmployeeByCWE('111111', "18-09-2016", '111111')
    .then(function(log) {
        return updateLoginTimeTo(log, '08-8668', '230993334');
    }, function(e) {
        return createNewEmployeeLog('224314', "18-09-2016", '230993334', '08-99');
    });
}, function(e) {
    return createEmployeeLoginBy('111111', "18-09-2016", '111111');
})
.then(SaveEmployeeLogToDb)
.then(DisplayLog)
.catch(function(e) {
    console.log(e);
});

That should work with the proviso that a log object is always delivered to SaveEmployeeLogToDb via all possible paths to that point, as implied by the original code.

Roamer-1888
  • 19,138
  • 5
  • 33
  • 44
  • Mr @Roamer-1888 you mean that I should build my system well so that I wan't need the cancellation !!?? maybe it 's a solution but what if i need through coding to abort , should I rebuild the system for single code ?? – Mohammad Abdullah Sep 22 '16 at 19:23
  • thanks a lot for your advice Mr @Roamer-1888 , it works for me , I think the main Idea for a Promise is to build the system well , and if an error occurred it should be bubbled to be handled at the tail of the chaining – Mohammad Abdullah Sep 22 '16 at 19:56
0

You can't currently "cancel" promises. But you can use an exception for that purpose:

findEmployeeByCW('11111', "18-09-2016").
then(function () {
return findEmployeeByCWE('111111', "18-09-2016", '111111')
}, function () {
console.log('createEmployeeLoginBy')

//*** "return" added
return createEmployeeLoginBy('111111', "18-09-2016", '111111').
then(function (log) {
SaveEmployeeLogToDb(log)

//****
throw new Error('promise_exit');
//****

})
})
.then(function (log) {
return updateLoginTimeTo(log, '08-8668', '230993334')
}, function () {
return createNewEmployeeLog('224314', "18-09-2016",
'230993334', '08-99')
})
.then(SaveEmployeeLogToDb).then(DisplayLog).catch(function (e) {

//****
//Only log if it's not an intended exit
if(e.message != 'promise_exit'){
    console.log(e);
}
//****

})
Stas Parshin
  • 1,458
  • 1
  • 16
  • 21