Promise and node making variables available in different functions - javascript

I have three functions, I wish to make the variable from function one and two available in function three.
Function one
Bellow in function one I am trying to include that variable emailUser in resolve to use it in a third function.
var firstMethod = function() {
var promise = new Promise(function(resolve, reject){
setTimeout(function() {
app.post('/api/data', function (req, res) {
console.log(req.body);
var emailUser = req.body.email;
res.send(emailUser);
});
console.log('first method completed');
resolve({data: emailUser });
}, 2000);
});
return promise;
};
Second function
This function I am trying to pass api_key for use in the third function.
var secondMethod = function(someStuff) {
var promise = new Promise(function(resolve, reject){
setTimeout(function() {
nodePardot.PardotAPI({
userKey: 34535345,
email: fsf#dd.com,
password: fd3sv34f,
DEBUG: true
}, function (err, client) {
if (err) {
// Authentication failed
console.error("Authentication Failed", err)
} else {
// Authentication successful
var api_key = client.apiKey;
console.log("Authentication successful !", api_key);
}
});
console.log('second method completed');
resolve({newData: api_key});
}, 2000);
});
return promise;
};
Third function
This function I would like to access the variables from function one and two. I have included a console log so that i may see the variables printed ion my console.
I would also like to access these functions for use in this third function/ method.
var thirdMethod= function() {
var promise = new Promise(function(resolve, reject){
console.log('show both functions', emailUser, api_key);
}, 3000);
});
return promise;
};
firstMethod()
.then(secondMethod)
.then(thirdMethod);

You need to resolve inside the function body when the API call has get response.
var firstMethod = function() {
var promise = new Promise(function(resolve, reject){
setTimeout(function() {
app.post('/api/data', function (req, res) {
console.log(req.body);
var emailUser = req.body.email;
res.send(emailUser);
//resolve when get the response
resolve({data: emailUser });
});
}, 2000);
});
return promise;
};
You must need to resolve or reject when error. Here I resolve the error and set api_key as empty.
var secondMethod = function(someStuff) {
var promise = new Promise(function(resolve, reject){
setTimeout(function() {
nodePardot.PardotAPI({
userKey: 34535345,
email: fsf#dd.com,
password: fd3sv34f,
DEBUG: true
}, function (err, client) {
if (err) {
// Authentication failed
console.error("Authentication Failed", err);
resolve({newData: ''});
} else {
// Authentication successful
var api_key = client.apiKey;
console.log("Authentication successful !", api_key);
resolve({newData: api_key});
}
});
}, 2000);
});
return promise;
};
function thirdMethod(result) {
console.log('show both functions', result[0].data, result[1].newData);
};
Promise.all([firstMethod(), secondMethod()])
.then(thirdMethod);
For Reference
var firstMethod = function() {
var promise = new Promise(function(resolve, reject) {
setTimeout(function() {
//resolve when get the response
resolve({
data: "a"
});
});
}, 2000);
return promise;
};
var secondMethod = function() {
var promise = new Promise(function(resolve, reject) {
setTimeout(function() {
//resolve when get the response
resolve({
data: "b"
});
});
}, 2000);
return promise;
};
var thirdMethod = function(result) {
console.log(result[0].data, result[1].data);
};
Promise.all([firstMethod(), secondMethod()]).then(thirdMethod);
Output:
a b

First you need to fix firstMethod and secondMethod. In firstMethod the emailUser variable is only set inside the callback function, it does not exist where you try to use it to resolve the promise. You need to move the promise resolution inside the callback.
Likewise in secondMethod the variable api_key only exists in the callback, and then only if the function succeeded. You need to put calls to resolve() and reject() inside that callback and remove the resolve() outside.
Once you have done that you should be resolving both promises with the correct values and your code can be:
function thirdMethod([emailUSer, api_key]) {
console.log('show both functions', emailUser, api_key);
};
Promise.all([firstMethod(), secondMethod()])
.then(thirdMethod);
Note that as thirdMethod is only called by then() you don't need to create a Promise unless you have something asynchronous to do: anything it returns will be automatically wrapped in a Promise for you.

your first promise resolves
{data: emailUser }
therefore somestuff in the second promise would be that
in your second method, you could resolve
{data: somestuff.data, newData: api_key}
then the third method can be written
var thirdMethod= function(values) {
and it would have the data from the first two promises
Overall, your code can be written (ES2016+)
var firstMethod = () => new Promise((resolve, reject) => setTimeout(() => app.post('/api/data', (req, res) => {
console.log(req.body);
var emailUser = req.body.email;
res.send(emailUser);
resolve({ emailUser });
}), 2000));
var secondMethod = ({ emailUser }) => new Promise((resolve, reject) => setTimeout(() => nodePardot.PardotAPI({
userKey: 34535345,
email: 'fsf#dd.com',
password: 'fd3sv34f',
DEBUG: true
}, (err, client) => {
if (err) {
// Authentication failed
console.error("Authentication Failed", err);
reject(err);
} else {
// Authentication successful
var api_key = client.apiKey;
console.log("Authentication successful !", api_key);
resolve({ emailUser, api_key });
}
}), 2000));
var thirdMethod = ({ emailUser, api_key }) => new Promise((resolve, reject) => setTimeout(() => {
console.log('show both functions', emailUser, api_key);
resolve('done');
}, 3000));
firstMethod().then(secondMethod).then(thirdMethod);

Related

How do I consume this second promise?

Here's the code
console.log("Before");
const p = getUser(1);
p.then(user => {
console.log(user);
getRepositories(user.github);
}).then(repos => {
console.log(repos);
});
console.log("after");
function getUser(id) {
return new Promise(function(resolve, reject) {
setTimeout(() => {
console.log("Calling a database " + id);
resolve({ id: id, github: "Zain" });
}, 2000);
});
}
function getRepositories(username) {
return new Promise(function(resolve, reject) {
setTimeout(() => {
console.log(`Calling Api for ${username}`);
resolve(["repo1", "repo2", "repo3"]);
}, 2000);
});
}
I'm having trouble with consuming the promise returned by getRepositories() function. Above is my implementation but it doesn't work and returns undefined (instead of the array [repo1, repo2, repo3]).
Here's the output:
I want the array to return after logging "Calling Api for Zain" but the undefined is shown before it, but I don't know why, so I need help regarding this.
You need a return statement in your first .then:
p.then(user => {
console.log(user);
return getRepositories(user.github);
}).then(repos => {
console.log(repos);
});
There’s a special syntax to work with promises in a more comfortable fashion, called “async/await”. It’s surprisingly easy to understand and use.
async function init() {
console.log("Before");
const user = await getUser(1);
console.log(user);
const repos = await getRepositories(user.github);
console.log(repos);
console.log("after");
}
init();
async function getUser(id) {
return new Promise(function(resolve, reject) {
setTimeout(() => {
console.log("Calling a database " + id);
resolve({ id: id, github: "Zain" });
}, 2000);
});
}
async function getRepositories(username) {
return new Promise(function(resolve, reject) {
setTimeout(() => {
console.log(`Calling Api for ${username}`);
resolve(["repo1", "repo2", "repo3"]);
}, 2000);
});
}
For more information: https://javascript.info/async-await

How to use json response to create another URL. Nodejs

I have the question, below code:
The problem is:
How can I send each line from response promiseGetCitiesData to promiseGetInformationDataPerCity.
Can I do it in one async.each functions?
Now, I created multiple Promise functions. One general function, which one start the program - getDataAndCloseDb().
Also I used async.each to call promise function with array parameter - locationArray.
Now, I would like to send each line from json response to next promise function (create get url), and collect the general response.
const MongoClient = require("mongodb").MongoClient;
const request = require("request");
const async = require("async");
var locationsArray = [
'location1',
'location2',
'location3'
];
function promiseConnectToDatabase(urldb) {
return new Promise(function(resolve, reject) {
MongoClient.connect(urldb, (err, db) => {
if (err) {
console.log("MongoDb connection error.");
reject(err);
}
console.log("Connected to MongoDb.");
resolve(db);
});
});
}
function promiseGetCitiesData(location) {
return new Promise(function(resolve, reject) {
request({
url: `https://example.com/${location}`,
json: true
}, (error, response, body) => {
if (error) {
console.log("Error connection to url.");
reject();
}
console.log("location: " + location);
console.log({location: location, cities: body.result.cities});
resolve({location: location, cities: body.result.cities});
});
});
}
/*
Example response from promiseGetCitiesData:
Location: location1
{ location: 'location1',
cities:
[ 'information1',
'information2',
'information3',
'information4'' ] }
*/
function promiseGetInformationDataPerCity(location, cities) {
return new Promise(function(resolve, reject) {
request({
url: `https://example.com/${location}/${cities}`,
//f.e https://example.com/location1/information1 etc.
json: true
}, (error, response, information) => {
if (error) {
console.log("Error connection to url.");
reject();
}
console.log(information);
resolve(information);
});
});
}
function promiseSaveDataToDatabase(db, body) {
return new Promise(function(resolve, reject) {
db.collection("testlocation").insert(body, function(dbError) {
if (dbError) {
reject(dbError);
}
resolve()
});
});
}
function promiseDisconnectDatabase(db) {
return new Promise(function(resolve, reject) {
db.close((err) => {
if (err) {
console.log("MongoDb disconnect error.");
reject(err);
}
console.log("MongoDb disconnected.");
resolve();
});
});
}
function promiseProvideDataFromEach(locationsArray, db) {
return new Promise(function(resolve, reject) {
async.each(locationsArray, function(loc, locProcessedCb) {
promiseGetcitiesData(loc).then(function(resultscities) {
promiseGetInformationDataPerCity(loc, resultscities).then(function(resultDetails) {
promiseSaveDataToDatabase(db, resultDetails).then(function() {});
locProcessedCb();
});
});
}, function(err) {
if (err) {
locProcessedCb(err);
reject(err);
}
console.log("All locations have been processed.");
resolve();
});
});
}
function getDataAndCloseDb() {
return new Promise(function(resolve, reject) {
promiseConnectToDatabase("mongodb://127.0.0.1:27017/testApp").then(function(db) {
promiseProvideDataFromEach(locationsArray, db).then(function() {
promiseDisconnectDatabase(db).then(function() {});
});
});
});
}
getDataAndCloseDb();
I think this is a lot simpler than the code in the question makes it appear. In particular, new Promise(...) can be completely avoided by :
using require('async-request') instead of require('request').
allowing MongoDb methods to return Promise, as many of them will do if no callback is passed.
Also
by using the Promise.all(array.map(...)) pattern the need for require('async') disappears.
https://stackoverflow.com/a/28915678/3478010 - provides a great little reusable disposer utility, which is useful here.
Remember to return a promise/value from every .then() callback that is itself asynchronous and/or should deliver data.
With some guesswork, I think you want something like this :
const MongoClient = require('mongodb').MongoClient;
const request = require('async-request'); // just like `request()` but returns a promise
var locationsArray = [
'location1',
'location2',
'location3'
];
function promiseGetCitiesData(loc) {
return request({
url: `https://example.com/${loc}`,
json: true
}).then(body => body.result.cities);
}
function promiseGetInformationDataPerCity(loc, cities) {
return Promise.all(cities.map(city => {
return request({
'url': `https://example.com/${loc}/${city}`,
'json': true
}).then(cityInfo => ({ 'name':city, 'info':cityInfo }));
}));
}
function promiseProvideDataFromEach(locationsArray, db) {
return Promise.all(locationsArray.map(loc => {
return promiseGetCitiesData(loc)
.then(cities => promiseGetInformationDataPerCity(loc, cities)
.then(citiesWithCityInfo => ({ 'location':loc, 'cities':citiesWithCityInfo }));
}))
.then(resultDetails => db.collection('testlocation').insertMany(resultDetails));
}
// disposer utility - credit: https://stackoverflow.com/a/28915678/3478010
function withDb(work) {
var _db;
return MongoClient.connect("mongodb://127.0.0.1:27017/testApp")
.then((db) => {
_db = db; // keep reference
return work(db); // perform work on db
}).finally(() => {
if (_db)
_db.close();
});
}
withDb(db => promiseProvideDataFromEach(locationsArray, db))
.then(() => {
// connection released here
});
The guesswork centres mainly around what is to be inserted at db.collection('testlocation').insertMany(resultDetails). The code in the question gives no more than a clue. My attempt seems reasonable but may not be exactly what you want. Be prepared to make some changes in promiseProvideDataFromEach() and promiseGetInformationDataPerCity().
you can do something like this. Its a simpler code but I think you can map it to your current code.
const Promise = require('bluebird')
const cities = ['citya', 'cityb', 'cityc']
function resolveCities() {
return new Promise(function(resolve, reject) {
resolve(cities)
})
}
function logCity(city) {
console.log('city ', city)
}
return resolveCities()
.then(function(cities) {
return Promise.mapSeries(cities, function(city) {
logCity(city);
});
})

Promises not waiting for each other in my nodejs controller

I got 4 promises here, and I thought that it would run the first one, then wait until its finished, THEN run the next one, wait till finished and THEN run the next one etc..
But what happens here is that it runs all of them all at once and does not wait for anything to finish.
This is my promise chain:
//
// Run the promises
//
findBanks
.then(findReceipts)
.then(findExpenses)
.then(sendResult)
.catch(err => {
console.error(err);
console.log("getbankAccountReport ERR: " + err);
res.json({error:true,err})
});
This is the output from my console.log
=====findAllBank=====
=====findAllReceipt=====
=====findAllExpense=====
=====RESOLVE findAllBank=====
=====sendResult=====
=====RESOLVE sendResult=====
=====RESOLVE findAllReceipt=====
=====RESOLVE findAllExpense=====
Am I not understanding promises correct or?
Anyway here is my nodejs controller:
exports.getBankAccountReport = function(req, res) {
//
// Find all bank accounts
//
var bankModel = require('../models/bankModel');
var bankTable = mongoose.model('bankModel');
var bankArray = [];
var findAllBank = new Promise(
(resolve, reject) => {
console.log("=====findAllBank=====")
bankTable.aggregate([
...lots of mongo stuff...
],function(err, data) {
if (!err) {
bankArray = data;
console.log("=====RESOLVE findAllBank=====")
resolve(data);
} else {
reject(new Error('findBank ERROR : ' + err));
}
});
});
//
// Find the RECEIPT for each bank account
//
var receiptModel = require('../models/receiptModel');
var receiptTable = mongoose.model('receiptModel');
var receiptArray = [];
var findAllReceipt = new Promise(
(resolve, reject) => {
console.log("=====findAllReceipt=====")
receiptTable.aggregate([
...lots of mongo stuff...
], function (err, data) {
if (!err) {
receiptArray = data;
console.log("=====RESOLVE findAllReceipt=====")
resolve(data);
} else {
reject(new Error('findReceipts ERROR : ' + err));
}
});
});
//
// Find the EXPENSE for each bank account
//
var expenseModel = require('../models/expenseModel');
var expenseTable = mongoose.model('expenseModel');
var expenseArray = [];
var findAllExpense = new Promise(
(resolve, reject) => {
console.log("=====findAllExpense=====")
expenseTable.aggregate([
...lots of mongo stuff...
], function (err, data) {
if (!err) {
expenseArray = data;
console.log("=====RESOLVE findAllExpense=====")
resolve(data);
} else {
reject(new Error('findExpense ERROR : ' + err));
}
});
});
var sendResult = function(data) {
var promise = new Promise(function(resolve, reject){
console.log("=====sendResult=====")
res.json({error:false,
"bank":bankArray,
"receipt":receiptArray,
"expense":expenseArray})
console.log("=====RESOLVE sendResult=====")
resolve();
});
return promise;
};
//
// Run the promises
//
findAllBank
.then(findAllReceipt)
.then(findAllExpense)
.then(sendResult)
.catch(err => {
console.error(err);
console.log("getbankAccountReport ERR: " + err);
res.json({error:true,err})
});
}
You need to wrap your Promises in functions
var findAllBank = function() {
return new Promise(
(resolve, reject) => {
console.log("=====findAllBank=====")
bankTable.aggregate([
...lots of mongo stuff...
],function(err, data) {
if (!err) {
bankArray = data;
console.log("=====RESOLVE findAllBank=====")
resolve(data);
} else {
reject(new Error('findBank ERROR : ' + err));
}
});
});
});
When resolved, the next function in the chain will be called with the data passed in the resolve() function.
Do no confuse the Promise and the function that builds it
When you create a new Promise(executor), you instanciate a new object that will have two methods (functions of an object), .then(resolveCB [, rejectCB]) and .catch(rejectCB).
The aime is to know, whenever a process is done, if it was successful or if it failed, and the continue accordingly.
var myFirstPromise = new Promise(function executor(resolve, reject) { resolve('resolved'); });
In other words, those methods are used to continue your process once the promise defined by executor is settled. It can either get fulfilled and call the resolveCB callback (usingthen), or rejected and call the rejectCBcallback (using both then and catch). A callback (resolveCB or rejectCB) is a function, not a Promise itself, even if the callback might return a Promise.
myFirstPromise
.then(function resolveCB(result) { console.log(result); }) //you can use a 2nd callback for rejection at this point
.catch(function rejectCB(err) { console.log(err); });
myFirstPromise
.then(
function resolveCB(result) { console.log(result); } // if resolved
, function rejectCB(err) { console.log(err); } // if rejected
)
.catch(function rejectCB(err) { console.log(err); }); // NEVER forget the last catch, just my 2cents :)
We saw the inputs of .then() and .catch() but what about their return value? The both of them will return a new Promise. That's why you can chain the .then()'s and .catch()'s.
myFirstPromise
.then(function resolveCB1(result) { console.log(result); })
.then(function resolveCB2(result) { console.log(result); }) // console.log is called but result is undefined
.catch(function rejectCB1(err) { console.log(err); });
myFirstPromise
.then(function resolveCB3(result) {
throw 'I threw an exception'; // an exception is thrown somewhere in the code
console.log(result);
})
.then(function resolveCB4(result) { console.log(result); })
.catch(function rejectCB2(err) { console.log(err); }); // a promise in the chain get rejected or error occured
In the previous example, we see that our second .then() is hit but result is undefined. The promise returned by first .then() fullfiled but no value as been passed by the executor to the resolve callback resolveCB2. In the second case, an exception occured in resolveCB3, it gets rejected so rejectCB2 is called. If we want our resolve callbacks to receive an argument, we have to notify the exector. To do so, the simplest way is for the callbacks to return a value:
myFirstPromise
.then(function resolveCB1(result) {
console.log(result);
result += ' again';
return result;
})
.then(function resolveCB2(result) {
console.log(result);
return result;
})
.catch(function rejectCB1(err) { console.log(err); });
Now that's said, you've got all the pieces together for understanding a Promise. Let's try to sum it up in a cleaner way:
var myFirstPromise = new Promise(function executor(resolve, reject) { resolve('resolved'); })
, resolveCB = function resolveCB(result) {
console.log(result);
result += ' again';
return result;
}
, resolveLastCB = function resolveLastCB(result) {
console.log(result);
result += ' and again';
return result;
}
, justLog = function justLog(result) {
console.log(result);
return result;
}
;
myFirstPromise
.then(resolveCB)
.then(resolveLastCB)
.then(justLog)
.catch(justLog);
You can now chan them nicely, it's cool and all
myFirstPromise
.then(resolveCB)
.then(resolveCB)
.then(resolveCB)
.then(resolveCB)
.then(resolveCB)
.then(resolveCB)
.then(resolveLastCB)
.then(justLog)
.catch(justLog);
But what if your Promise chain 'really' changes and you need to get rid of myFirstPromise and start with resolveCB instead ? It's just a function, it can be executed but doesn't have any .then() or .catch() method. It's not a Promise. You can't do resolveCB.then(resolveLastCB), it will thow an error resolveCB.then( is not a function or something similar. You might think this is a gross mistake, I didn't call resolveCB and resolveCB().then(resolveLastCB) should work? Unfortunalty for those who thought about that, it's still wrong. resolveCB returns a string, some characters, not a Promise.
In order to avoid this kind of maintenance issue, you should know that the resolve and reject callbacks can return a Promise instead of a value. To do so, we'll use what's called the factory pattern. In simple words, the factory pattern is about instanciating new object using a (static) function instead of using the constructor directly.
var myFirstPromiseFactory = function myFirstPromiseFactory() {
/*
return new Promise(function executor(resolve, reject) {
resolve('resolved');
});
if you just need to resolve a Promise, this is a quicker way
*/
return Promise.resolve('resolved');
}
, resolveFactory = function resolveFactory(result) {
return new Promise(function executor(resolve, reject) {
result = result || 'I started the chain';
console.log(result);
result += ' again';
return resolve(result); // you can avoid the return keyword if you want, I use it as a matter of readability
})
}
, resolveLastFactory = function resolveLastFactory(result) {
return new Promise(function executor(resolve, reject) {
console.log(result);
result += ' and again';
return resolve(result);
});
}
, justLogFactory = function justLogFactory(result) {
return new Promise(function executor(resolve, reject) {
console.log(result);
return resolve(result);
});
}
;
myFirstPromiseFactory() //!\\ notice I call the function so it returns a new Promise, previously we started directly with a Promise
.then(resolveFactory)
.then(resolveLastFactory)
.then(justLogFactory)
.catch(justLogFactory);
// Now you can switch easily, just call the first one
resolveFactory()
.then(resolveLastFactory)
.then(justLogFactory)
.catch(justLogFactory);
justLogFactory('I understand Javascript')
.then(resolveLastFactory)
.then(justLogFactory)
.catch(justLogFactory);
Factory functions might come handy when iterating over an array. It might be used to produce an array of promise given an input:
var myFirstPromiseFactory = function myFirstPromiseFactory() {
/*
return new Promise(function executor(resolve, reject) {
resolve('resolved');
});
if you just need to resolve a Promise, this is a quicker way
*/
return Promise.resolve('resolved');
}
, resolveFactory = function resolveFactory(result) {
return new Promise(function executor(resolve, reject) {
result = result || 'I started the chain';
console.log(result);
result += ' again';
return resolve(result); // you can avoid the return keyword if you want, I use it as a matter of readability
})
}
, resolveLastFactory = function resolveLastFactory(result) {
return new Promise(function executor(resolve, reject) {
console.log(result);
result += ' and again';
return resolve(result);
});
}
, justLogFactory = function justLogFactory(result) {
return new Promise(function executor(resolve, reject) {
console.log(result);
return resolve(result);
});
}
, concatValues = function concatValues(values) {
return Promise.resolve(values.join(' '));
}
, someInputs = [
'I am an input'
, 'I am a second input'
, 'I am a third input'
, 'I am yet an other input'
]
;
myFirstPromiseFactory() //!\\ notice I call the function so it returns a new Promise, previously we started directly with a Promise
.then(resolveFactory)
.then(resolveLastFactory)
.then(justLogFactory)
.catch(justLogFactory);
// Now you can switch easily, just call the first one
resolveFactory()
.then(resolveLastFactory)
.then(justLogFactory)
.catch(justLogFactory);
justLogFactory('I understand Javascript')
.then(resolveLastFactory)
.then(justLogFactory)
.catch(justLogFactory);
// Using a factory functions to create an array of promise usable with Promise.all()
var promiseArray = someInputs.map(function(input) {
return justLogFactory(input);
});
Promise.all(promiseArray)
.then(concatValues)
.then(resolveLastFactory)
.then(justLogFactory)
.catch(justLogFactory);

How to "transfer" variables down the promise chain in node express router?

I am trying to make two separate DB calls in a promise chain, although for testing purposes, the first call is replaced by a simple string that gets passed along.
My problem is that I can't access the variable msg in my second promise (where I try to set context.foo = msg.
router.route("/")
.get(function(request, response) {
var session = request.session;
return new Promise(function(resolve, reject) {
resolve("h!");
}).then(function(msg){
return new Promise(function(resolve, reject) {
Snippet.find({}, function(error, data) {
let context = {
snippets: data.map(function(snippet) {
return {
name: snippet.name,
snippet: snippet.snippet,
createdAt: snippet.createdAt,
user: snippet.user,
id: snippet._id
};
}),
foo: msg
};
resolve(context);
});
});
}).then(function(context){
response.render("start/index", context);
}).catch(function(err){
response.end(err);
});
});
Another attempt, here trying to bind the router to the promise...
router.route("/")
.get(function(request, response) {
var session = request.session;
return new Promise(function(resolve, reject) {
resolve("hi!");
}).then(function(msg){
router.msg = msg;
return new Promise(function(resolve, reject) {
Snippet.find({}, function(error, data) {
let context = {
snippets: data.map(function(snippet) {
return {
name: snippet.name,
snippet: snippet.snippet,
createdAt: snippet.createdAt,
user: snippet.user,
id: snippet._id
};
}),
foo: this.msg
};
resolve(context);
}.bind(router));
});
A third attempt...
router.route("/")
.get(function(request, response) {
var session = request.session;
return new Promise(function(resolve, reject) {
context.msg = "hi!";
resolve(context);
}).then(function(context){
return new Promise(function(resolve, reject) {
Snippet.find({}, function(error, data) {
context.snippets = {
snippets: data.map(function(snippet) {
return {
name: snippet.name,
snippet: snippet.snippet,
createdAt: snippet.createdAt,
user: snippet.user,
id: snippet._id
};
}),
};
resolve(context);
});
});
}).then(function(context){
response.render("start/index", context);
}).catch(function(err){
response.end(err);
});
});
So the basic problem is always, how can I "inject" or make use of a variable inside the Promise scope, when I have no surrounding object, no "this" to attaach it to =)
Using bluebird's powerful Promise.coroutine():
router.route("/")
.get(Promise.coroutine(function*(request, response) {
try {
const dbResult1 = yield getValueFromDatabaseAsync(/* w/e */);
const dbResult2 = yield getValueFromDatabaseAsync(/* w/e, can be dbResult1 */);
const context = {...}; // use dbResult1 and dbResult2 here normally
response.render("start/index", context);
} catch (err) {
response.end(err);
}
}));

Conditional on Promise.all [duplicate]

This question already has answers here:
Wait until all promises complete even if some rejected
(20 answers)
Closed 7 years ago.
I'm aware of this answer and the following code is based on it. It is working but I'm not happy with something.
What if I want to make some conditional checking and resolve based on it? I've commented on code where I'd like to change.
/**
* Returns not yet propagate subdomains.
* #return Promise
*/
Tour.prototype.getOffline = function() {
var self = this;
var resolve_, reject_;
// to be fulfilled later
var promise = new Promise(function(resolve, reject){
resolve_ = resolve;
reject_ = reject;
});
var check = function(key) {
return new Promise(function(resolve, reject) {
redis_client.hmget(key, 'online', function(err, reply) {
if (reply[0] === 'n') {
resolve(key); // <----- I'd like to resolve only here
} else {
// reject(); // <---- I can't reject cause of Promise.all
resolve(); // <----- An empty value to satisfy Promise.all
}
});
});
};
this.getKeysRedis('subdomain:*').then(function(resp) {
var promises = resp.map(check);
var results = Promise.all(promises);
results.then(function(data) {
// I have undefined values on data array
var array = data.filter(function(key){
return utils.isDefAndNotNull(key);
});
resolve_(array); // <------ How can I resolve without filtering
});
}, function(error) {
reject_(error);
});
return promise;
};
Final code, removed Promise constructor antipattern as #Bergi alerted:
Tour.prototype.getOffline = function() {
var self = this;
var check = function(key) {
return new Promise(function(resolve, reject) {
redis_client.hmget(key, 'online', function(err, reply) {
if (reply[0] === 'n') resolve(key);
else reject();
});
});
};
var reflect = function(promise) {
return promise.then(
x => ({state: 'resolved', value: x}),
e => ({state: 'rejected' , value: e})
);
};
return new Promise(function(resolve, reject) {
self.getKeysRedis(self.subdomain_keyname + '*')
.then(function(keys) {
return Promise.all(keys.map(check).map(reflect)).then(function(r) {
return r.filter(x => x.state === 'resolved');
});
})
.then(function(results) {
var array = results.map((result) => result.value);
resolve(array);
})
.catch((err) => reject(err));
});
};
You basically want a function that takes a promise and returns a promise that fulfills whenever the other promise resolves (fulfills or rejects):
That function is typically called "reflect":
function reflect(promise){
return promise.then(x => ({state: "fulfilled", value: x}),
e => ({state: "rejected" , value: e}));
}
This means you can reject (like you normally would) and then do:
this.getKeysRedis('subdomain:*').then(resp =>
resp.map(check).map(reflect); // note the reflect
).then(Promise.all).then(results => {
// access results here
results[0].state; // "fulfilled" if it fulfilled
results[0].value; // the value or error
});

Categories