Hi I am new to ES6 and I am using promise chain
I am not getting error catch in my promise chain.
let cost, stars;
getStarInfo(req.body.star_id).then( (star) => {
let stripe_object = new Stripe(req.body.stripe_token, cost);
return stripe_object.makepayment();
}).then( (payment) => {
console.log(1 ,payment);
return savePurchase(req.decoded._id, cost, stars, payment.id);
}).catch( (err) => {
res.json({'success' : false , 'err' : err , msg : 'Something went wrong please try again'});
});
My savePurchase function is like this
function savePurchase( cost , stars, payment_id){
console.log("hello")
return new Promise( (resolve, reject) => {
var purchasedstars = new Stars({
user_id : user_id,
stars : stars,
money_earned : cost,
transaction_id : payment_id
});
console.log(purchasedstars)
purchasedstars.save(function(err , saved_doc){
console.log('save' , err , saved_doc)
if(err){
reject(err)
}else{
resolve(saved_doc);
}
});
});
}
In savePurchase function if my user_id is undefined, the promise does not give me error. It just goes in the catch and give empty error object. How can I find out the error in my function.?
After returning a new promise from savePurchase you chaining your catch with it, but not with getStarInfo promise anymore, so you have no error handler for getStarInfo promise.
.then() takes an optional second function to handle errors
var p1 = new Promise( (resolve, reject) => {
resolve('Success!');
// or
// reject ("Error!");
} );
p1.then( value => {
console.log(value); // Success!
}, reason => {
console.log(reason); // Error!
} );
Define custom error and reject.
purchasedstars.save(function (err, saved_doc) {
console.log('save', err, saved_doc)
if (err) {
reject({
err,
message: 'Some error message'
})
} else {
resolve(saved_doc);
}
});
Related
So I have the following promise that is supposed to go inside my sqlite database and tell me if there is a match. Even if the variables are correct it always returns undefined.
var checkLoanable = function(bookID){
return new Promise((resolve, reject)=>{
db.run('SELECT * FROM book WHERE id=?',[bookID],(err,row)=>{
if (err) {
console.log("error")
reject();
}
if(row){
if(row.loanable==0){
console.log("not loanable")
reject();
}
console.log("success")
resolve(row);
}else{
console.log("undefined")
reject();
}
console.log("End of promise")
});
})
}
This is the call of the function
await checkLoanable(bookID)
.then(()=>{console.log("success")})
.catch(()=>{
res.status(422)
.setHeader('content-type', 'application/json')
.send({ message: "Failed to add loan: book is not loanable!"});
});
console.log("after promise");
The await is inside an async function and after tests I did it does wait for the promise to end and then continues the code. I can't figure out why its always undefined though.
Thank you in advance for any help.
run() is used for inserting and updating rows.
For getting a single row use db.get().
db.get('SELECT * FROM book WHERE id=?', [bookID] , (err,row) => { });
At least if you are using the npm/sqlite package.
Do not mix async/await and Promise chain use one of them.
Either with try...catch or Promise..then..catch
try {
const book = await checkLoanable(bookID);
res.json(book);
}catch ({message}) {
res.status(422).send(message);
}
// or
checkLoanable(bookID)
.then((book) => {res.json(book);})
.catch(({message}) => {
res.status(422).json({
message: "Failed to add loan: book is not loanable!"
});
});
For the example if you use something like this
const myPromise = () => new Promise((resolve, reject) => {
setTimeout(() => {
resolve('Success!');
}, 1000);
});
(async () => {
const result = await myPromise().then(data => {
console.log({data});
});
console.log({result});
})();
Result would be
{data: 'Success!'}
{result: undefined}
Not both get values at the same time.
I am working with some own built promise examples to understand how this function is working.
That following code produces the error
ReferenceError: reject is not defined
I start this with node Promise.js and use node version 8.11.3
Here is my code, the part which produces the error is commented with "problem
function testPromise () {
//part 1
function checkCountOnServer () {
return new Promise(function (resolve, reject) {
var available = false
if (available) {
resolve('available')
}
else {
reject('not available')
}
})
}
function checkPayment () {
return new Promise(function (resolve, reject) {
var booleanTest = true
if (booleanTest) {
resolve('payment done')
}
else {
reject('no payment received')
}
})
}
var checkCountOnServerVar = checkCountOnServer()
checkCountOnServerVar.then(function (resolve) {
console.log(resolve)
return resolve(checkPayment())
}, function (reason) {
console.log(reason) //works
reject(reason) // problem
}).then(function (value) { console.log(value) },
function (rejected) {
console.log(rejected) //problem
})
}
testPromise()
I actually expect the message 'not available' two times.
Even if I change reject(reason) to reject('test') I get the same error.
Help me please.
checkCountOnServerVar.then(function (resolve) {
The then callback is called with whatever value the promise was resolved to, which is "payment done" in your case,and that is not a function, so you can't call it. To chain a promise from inside a then handler, just return it:
checkCountOnServerVar.then(function (status) {
console.log(status);
return checkPayment();
})
Additionally your error catcher does not make sense at all,
function (reason) {
console.log(reason) //works
reject(reason) // problem
}
As reject is not defined, and as you actually don't handle the error. If you don't handle the error, there is no sense in attaching a handler, otherwise you should return a value the chain can continue with, such as:
function(error) {
console.error("Something bad happened", error);
return "An error occured, but we don't mind...";
}
To sum up:
checkCountOnServer()
.then(serverCount => checkPayment())
.then(payment => console.log(payment))
.catch(error => console.error(error));
I'm trying to test a function which recives a promise like this
{
state: 'fulfilled'
value:[Array]
}
function register (foo) {
return new Promise((resolve,reject)=>{
query = "INSERT IGNORE INTO....";
connection.query(query, [foo.value.from, foo.value.id], (err, res, fields) => {
if(err){
return undefined //this will change
}
return foo
})
})
}
The thing is I am returning, non rejecting neither resolving. So when Im testing..
it('insertion error', function () {
var res = 'error'
connection = {
query: (query, input, cb) =>{
return cb(res, null, null)
}
}
let database = proxyquire('path/to',{
'./connection': connection
})
var input =
{
value: {
id: 'bar',
data: [],
from: 'foo'
}}
return database.register(input)
.then( (result) => {
expect(result).to.be.undefined
})
.catch( (err) => {
console.log(err)
err = new Error (`Test fail: ${err}`)
throw err;
})
})
The function works well, Im pretty sure of that.
The other thing I'm sure of is that the THEN/CATCH condition never shows up. I know if I replace the return for resolve it will show up, but I need to be returned.
What should I modify, or how should I test it.
This is a basic example how can you make a promisse, in this example you can send true or false in the register() function.
function register ( foo ) {
return new Promise( (resolve, reject) =>{
if(foo){
resolve('Done');
}else{
reject('Error')
}
} )
}
register(false).then( result=>{
document.write('promisse success... ', result);
} ).catch( error => {
document.write('promisse error... ', error);
} )
I have a helper function for using fetch with CouchDB which ends as:
...
return fetch(...)
.then(resp => resp.ok ? resp.json() : Promise.reject(resp))
.then(json => json.error ? Promise.reject(json) : json)
and when I use it elsewhere, I was under the impression that I could .catch those explicit rejections:
above_function(its_options)
.then(do_something)
.catch(err => do_something_with_the_json_error_rejection_or_resp_not_ok_rejection_or_the_above(err))
but alas, I can't seem to be able to get a hold of the rejections.
The specific error I'm after is a HTTP 401 response.
What gives?
(Please note that there are implicit ES6 return's in the .thens)
function test() {
return new Promise((resolve, reject) => {
return reject('rejected')
})
}
test().then(function() {
//here when you resolve
})
.catch(function(rej) {
//here when you reject the promise
console.log(rej);
});
Make sure every call to a then() returns a value.
For e.g.
var url = 'https://www.google.co.in';
var options = {};
var resolves = Promise.resolve();
resolves.then(() => {
console.log('Resolved first promise');
var fetchPromise = fetch(url, options);
fetchPromise.then(() => {
console.log('Completed fetch');
});
})
.catch(error => {
console.log('Error', error);
});
Notice the console shows an uncaught exception. However, if you returned the inner promise (or any other value, which ends up turning into a promise via resolve), you end up flattening the promise so exception bubble up.
var url = 'https://www.google.co.in';
var options = {};
var resolves = Promise.resolve();
resolves.then(() => {
console.log('Resolved first promise');
var fetchPromise = fetch(url, options);
return fetchPromise.then(() => {
console.log('Completed fetch');
});
})
.catch(error => {
console.log('Error', error);
});
Notice the exception bubbles up to the outer promise. Hope this clears up things a little bit.
Why not wrap it in a try / catch block
// define a failing promise
const test = ()=> new Promise((resolve, reject) => reject('rejected'));
// using an immediately executing function to call an async block
(async ()=> {
try {
await test(); // => this will throw an error
} catch (er) {
console.log(er); // 'rejected'
}
})();
Promise rejections fall to the second param of the then function.
function test() {
return new Promise((resolve, reject) => {
return reject('rejected')
})
}
test().then(function() {
//here when you resolve
}, function(rej) {
//here when you reject the promise
console.log(rej)
})
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);