Created
April 17, 2014 21:11
-
-
Save chrisabrams/11011880 to your computer and use it in GitHub Desktop.
Bluebird promise chain example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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