Skip to content

Instantly share code, notes, and snippets.

@chrisabrams
Created April 17, 2014 21:11
Show Gist options
  • Star 24 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save chrisabrams/11011880 to your computer and use it in GitHub Desktop.
Save chrisabrams/11011880 to your computer and use it in GitHub Desktop.
Bluebird promise chain example
Promise = require('bluebird')
var A = function() {
return new Promise(function(resolve, reject) {
var result = 'A is done'
console.log(result)
resolve(result);
})
}
var B = function() {
return new Promise(function(resolve, reject) {
var result = 'B is done'
setTimeout(function() {
console.log(result)
resolve(result);
}, 2000)
})
}
var C = function() {
return new Promise(function(resolve, reject) {
var result = 'C is done'
console.log(result)
resolve(result);
})
}
var D = function() {
return new Promise(function(resolve, reject) {
var result = 'D is done'
console.log(result)
resolve(result);
})
}
A()
.then(function(result) {
return B();
})
.then(C)
.then(D)
@arbazsiddiqui
Copy link

arbazsiddiqui commented Jul 19, 2017

suppose i chain promise A, B, C and the i want to use the result resolved by A in C. How do I do that?

@gbejic
Copy link

gbejic commented Aug 2, 2017

@arbazsiddiqui one of solution would be
var resultFromA; A() .then(function(result) { resultFromA = result; return B(); }) .then(function(resultB) { console.log(resultFromA); return C(); }) .then(D)

@xobjects
Copy link

Question..

above you wrote

A() .then(function(result) { return B(); }) .then(C) .then(D)

is this the same as

A() .then(B) .then(C) .then(D) ?

are there 'any' differences ?

@tavurth
Copy link

tavurth commented Sep 26, 2017

@gksource No important differences. then or catch will call the function you pass to it.

const shouldGiveError = false;

new Promise((resolve, reject) => {
    if (shouldGiveError)
        return reject("Oh no!, An error occurred!")

    resolve("This will be printed to the console on success");
})
.then(console.log)
.catch(console.error)

@tradingbills
Copy link

Thanks, glad you wrote this. The cloud is lifting

@allexon
Copy link

allexon commented Dec 21, 2018

Hello, I have two promise as functions and also other functions using bluebird, how do I now to chain the execution of promises and functions?

*** Promise 1 *****
function fntTotalDeRegistros () {
promise.using (db, () => {
db.query ('SELECT COUNT (*) FROM CUSTOMER')
.then ((rows) => {
rows.forEach ((row) => {
qtdRegsARQ = row.registers});
})
.then () => db.close ())
.catch ((error) => {console.log (error), db.close ()});
})
};
**** Promise 2 ***
function fntRegisterCli () {
promise.using (db, () => {
db.query ('Select * from DB_ICADIS.CLIENTE ORDER BY cli_codigo')
.then ((rows) => {
rows.forEach ((row) => {
seqCLI + = (+1);
regCLI = func.concatRight ('CLI', 3, '');
destinationCRL = func.concatRight (row.cli_destinator, 20, '');
socialCocial = func.concatRight (row.cli_rozao_social, 40, ");
addressCLI = func.concatRight (row.cli_endereco, 40, '');
neighborhoodCLI = func.concatRight (row.cli_boirro, 25, '');
cepCLI = func.concatRight (row.cli_cep, 8, '0');
municipalityCLI = func.concatRight (row.cli_municipality, 25, '');
ufCLI = func.concatRight (row.cli_uf, 2, ");
telCLI = func.concatRight (row.cli_phone, 8, '0');
cnpjCLI = func.concatRight (row.cli_cnpj, 14, '0');
inscEstCLI = func.concatRight (row.cli_insc_est, 18, '');
typeCloser = func.concatRight (row.cli_people_type, 1, '');
dataLastLastCLI = Func.dataFormaradaAnoMesDia (func.concatRight (row.cli_data_ultima_alt, 10, ''));

// CMP registry
fntRegisterCmp (row.cli_compl_end, seqCLI);

// *** CMP Registry Sequence Control ******
other SeqCMP = seqCLI;
if (seqCLI == seqCMP) {seqCLI + = +1}
other SeqCMP = func.concatLeft (seqCLI, 6, '0')

if (complEndCMP! == null)
{regsCLI + = $ {regCLI} {{CLI recipient} $ {CSocialSource} $ {CGIers} $ {neighborhoodCLI} $ {cepCLI} {municipalitiesCLI} $ {ufCLI} {telCLI} $ {cnpjCLI} $ {inscEstCLI} $ {typeClosedChart} {dataLastLastCLI} $ {seqCLI} \ n $ {regsCMP} \ n}
else
{regsCLI + = $ {regCLI} {{CLI recipient} $ {CSocialSource} $ {CGIers} $ {neighborhoodCLI} $ {cepCLI} {municipalitiesCLI} $ {ufCLI} {telCLI} $ {cnpjCLI} $ {inscEstCLI} $ {typeClosedChart} {dataUltimaLastCLI} $ {seqCLI} \ n}

}); // end end row
})
.then (() => console.log (regsCLI))
.then () => db.close ())
.catch ((error) => {console.log (error), db.close ()});
})
};

**** function 1 ****
function fntGreateArchiveTXT (param_string) {
formatNameName = ./files-txt/client/A${func.concatLeft(seqARQ, 7, '0')} .cli;
fs.writeFile (formatNameName, param_string, {encoding: 'utf-8', flag: 'a'}, function (err) {if (err) throw err; console.log ('File saved successfully')});
}

 ******* HOW TO DRAW ******
promise1
promise2
function1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment