I'm trying to write a function that i can use within a promise chain that changes the resolved value.
Below, I want function getAndChangeValue() to change the resolved argument from "Hello" to "Bye". Please help! I can't seem to get my head around it. :-)
https://plnkr.co/edit/RL1XLeQdkZ8jd8IezMYr?p=preview
getAndChangeValue().then(function(arg) {
console.log(arg) // I want this to say "Bye"
});
function getAndChangeValue() {
var promise = getValue()
promise.then(function(arg) {
console.log('arg:', arg) // says "Hello"
// do something here to change it to "Bye"
})
return promise
}
function getValue() { // returns promise
return $.ajax({
url: "myFile.txt",
type: 'get'
});
}
You can just return whatever value you like in the function passed to then(), but you will have to return the new promise returned by then() and not the original promise:
function getAndChangeValue() {
return getValue().then(function(arg) {
return "Bye";
}
}
If you are using BluebirdJS you are able to add .return(value).
E.g.:
var firstPromise = new Promise(function (resolve, reject) {
return resolve('FIRST-VALUE');
})
.then(function (response) {
console.log(response); // FIRST-VALUE
var secondPromise = new Promise(function (resolve) {
resolve('SECOND-VALUE');
});
// Here we are changing the return value from 'SECOND-VALUE' to 'FIRST-VALUE'
return secondPromise.return(response);
})
.then(function (response) {
console.log(response); // FIRST-VALUE
})
Related
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);
Below outlines a promise that has two simple steps display on the console something, then display something else after the first step is completed.
I am trying to understand how to resolve the promise, and allow the 2nd step to complete.
var lookup_documents = new Promise(
function(resolve, reject) {
console.log("1st");
//resolve(); - How do I do this outside of this function?
}
);
lookup_documents.then(
function() {
console.log("2nd");
}
);
Normally you use new Promise() when you want to wrap some asynchronous function into a promise, such as:
new Promise((resolve, reject) => {
makeAjaxCall((err, data) => {
if(err) return reject(err);
else return resolve(data);
})
});
There are other Promise implementations, such as q, where you can create a "deferred" object that returns a promise... Something similar would be:
function makeAjaxCallPromise() {
let deferred = Q.defer();
makeAjaxCall((err, data) => {
if(err) return deferred.reject(err);
else return deferred.resolve(data);
});
return deferred.promise;
}
For the specific thing you want... All I can think of is declare a variable outside of the promise function scope and asign it, something like:
let resolver, rejecter;
new Promise((resolve, reject) => {
resolver = resolve;
rejecter = reject;
});
makeAjaxCall((err, data) => {
if(err) return resolver(err);
else return rejecter(data);
});
But that doesn't look too good and can lead to errors... Try to wrap everything in your Promise function.
You can do it like this, using your example - if I understand you correctly.
var lookup_documents = function() {
return new Promise(function(resolve, reject) {
console.log("1st");
resolve();
});
};
lookup_documents().then(function() {
console.log("2nd");
});
I am calling a promise function in a loop and I want to be able to return all of its promises and return a value (assetIds) as well. However I am after calling the function (addToCollectionMap) obj.assetIds is undefined.
function addToCollectionMap(client, json, collectionId) {
var promises = [];
var assetIds = [];
return new Promise(function (resolve) {
var list = JSON.parse(json)["list"];
list.forEach(function (asset) {
var assetId = asset["id"];
assetIds.push(assetId);
promises.push(setToCache(client, PREFIX + assetId, collectionId));
});
promises.push(resolve({assetIds: assetIds}));
}).then(function () {
return Promise.all(promises);
});
}
Calling code:
return addToCollectionMap(client, jsonString, collectionId)
.then(function (obj) {
return setToCache(client, ASSET_MAP_PREFIX + collectionId, obj.assetIds);
});
Your .then is returning the promise created by Promise.all(promises), therefore it will have the results of all the promises. You simply need another .then that returns what you want instead.
}).then(function () {
return Promise.all(promises);
}).then(function () {
return assetIds;
});
I'm trying to return a value from a promise, but I can't. Why it doesn't work?
I get Promise { <pending> } output only.
function
function username() {
return new Promise(function (resolve, reject) {
user.where('id', req.id).fetch().then(function (data) {
data = data.toJSON();
resolve(data);
});
});
}
variable
var r = username().then(function (a) {
return a.username;
});
console.log(r);
If I remove return and put console.log(a.username), it works, but it's not the result I want. I want to put returned value inside r.
EDIT #1
I need to pass my returned values into a view (like below), so I must be able to access them outside of the then() chain.
res.render("frontend/index", {
value1 : value1,
value2 : value2,
value3 : value3
});
EDIT #2
I'm using Express.js
Now I get "Error: Can't set headers after they are sent." error, I hope it's more clear now. When a user tries to access a page, I query the database and pass variables to a view, but there are more than one operation per view (username, post info, comments, etc).
exports.index = function (req, res) {
function username() {
return new Promise(function (resolve, reject) {
user.where('id', req.id).fetch().then(function (data) {
data = data.toJSON();
resolve(data);
});
});
}
username().then(function (b) {
res.render('backend/index', {
username: b.username
});
});
function post() {
return new Promise(function (resolve, reject) {
post.where('id', req.params.postId).fetch().then(function (data) {
data = data.toJSON();
resolve(data);
});
});
}
post().then(function (a) {
res.render('backend/index', {
post: a.post
});
});
};
The .then() function returns a promise only. Here in above code variable r is nothing but a promise reference object.
If you want to use the returned response from the promise, this is how you will do it -
username().then(function (a) {
console.log(JSON.stringify(a));
// you will need to use the response returned from server here..
// display in view or something else
res.render("frontend/index", {
value1 : a.value1,
value2 : a.value2,
value3 : a.value3
});
});
Returning a value from inside a promise thenable function will only return a promise and not the value.
You will need to wait till all the promises are resolved and then only send the response from the server. Once a response is sent, you can not send it again and hence the error headers....
Updated answer as per modified question -
exports.index = function (req, res) {
function username() {
return new Promise(function (resolve, reject) {
user.where('id', req.id).fetch().then(function (data) {
data = data.toJSON();
resolve(data);
});
});
}
function post() {
return new Promise(function (resolve, reject) {
post.where('id', req.params.postId).fetch().then(function (data) {
data = data.toJSON();
resolve(data);
});
});
}
username().then(function (b) {
post().then(function (a) {
res.render('backend/index', {
post: a.post,
username: b.username
});
});
});
};
Try performing next process where fulfilled value of r is used inside of .then() to access asynchronous returned value from Promise r
var r = username().then(function (a) {
return a.username;
});
r.then(function(data) {
// `data` : `r` return value
// do stuff with `data` here
});
You'll have to restrict the usage of the value to functions passed to .then. There is no other logical way to assert that the value of r has been assigned.
var r;
username().then(function(a) {
r = a.username;
// use r here
// resolve promise with value of username
return r;
}).then(function(username) {
// or use it here
});
I've stepped into wonderful world of Promises a few days ago and I was just thinking I was enlightened. Promises look simple but they can be confusing.
Could you please tell me why the following test doesn't pass?
var Promise = require('bluebird');
var expect = require('chai').expect;
var request = Promise.promisifyAll(require('request'));
describe('Promise', function() {
it('should work again', function() {
var final_result;
function first_promise() {
return new Promise(function(resolve, reject) {
resolve("http://www.google.com");
})
}
function second_promise() {
return new Promise(function(resolve, reject) {
resolve("This is second promise!");
})
}
function inner_async_request(url_from_first_promise) {
return new Promise(function(resolve, reject) {
return request.getAsync(url_from_first_promise).spread(function(response, content) {
final_result = content;
resolve(content);
})
})
}
return request.getAsync('http://127.0.0.1:3000/').spread(function(result, content) {
//do something with content and then return first_promise
console.log(content);
return first_promise;
})
.then(function(url) {
inner_async_request(url).then(function(result) {
console.log(result);
final_result = result;
})
return second_promise;
})
.then(function(result) {
// result should be "This is second promise!"
console.log(result);
// final_result should be google's html
expect(final_result).not.to.be.undefined;
})
});
});
Currently the error is: Unhandled rejection Error: options.uri is a required argument which should be received from first_promise, I guess?
By this test, actually, I want to understand how to use promises which depend on each other AND how to use promises as asynchronous functions inside promises -which should just work separately-.
Thank you
You need to call the functions to return promises, like
return request.getAsync('http://localhost:3000/').spread(function(result, content) {
//do something with content and then return first_promise
console.log(content);
return first_promise();
})
And in some cases you don't need to create new promises at all
for Eg.
function inner_async_request(url_from_first_promise) {
return request.getAsync(url_from_first_promise).spread(function(response, content) {
final_result = content;
return content;
})
}
Finally, to make your test work, you need to modify this as well
.then(function(url) {
// return
return inner_async_request(url).then(function(result) {
console.log(result);
final_result = result;
return second_promise(); // return second promise inside then
})
})
DEMO