I have this simple method that i'm trying to test.
function isS3Healthy(s3, logger = console) {
return new Promise((resolve, reject) => {
s3.listObjects({}, (err, data) => {
if (err) {
logger.error(err)
return resolve(['s3', false])
}
return resolve(['s3', true])
})
})
}
My test keeps timing out and it has to do with my mocked object isn't right. What does this mock object need to look like in order to test my method properly?
describe.only('#isS3Healthy', () => {
let s3
before(() => {
s3 = {
listObjects: ({}, (err, data) => {
return Promise.resolve(['s3', true])
})
}
})
it('should return that it is healthy', () => {
return isS3Healthy(s3)
.then(result => {
const [name, status] = result
expect(name).to.equal('s3')
expect(status).to.be.true
})
.catch(err => {
console.log('err = ', err)
})
})
})
Based on the code you've provided, it looks like listObjects is a function that accepts an object and a callback, and at some point calls that callback, so probably the simplest way to mock it would be like this:
listObjects: (_, c) => c()
The function you're testing only seems to care whether listObjects is passing an error to the callback or not, so this should seemingly be sufficient.
Side note: you have return resolve in two places in your isS3Healthy function, but using return here almost certainly serves no purpose. To resolve a promise, just call resolve(...) without returning it.
Related
I´m new to JavaScript and a little bit confused about Promises, this is what I have:
export const testFunction = () => dispatch => {
otherFunction().then(( response ) => {
//do something...
return response;
}).catch(( error ) => {
//do something...
return error;
});
}
In another file I'm trying to get the value returned from the then like this:
let result = this.props.testFunction()
And like this:
let result = this.props.testFunction ().then(( result ) => {
console.log(result);
}).catch(( error ) => {
console.log(result); // undefined
});
But I get undefined as the result, what is the correct way of getting that value?
testFunction is not returning a Promise so you can't use then or catch and returns undefined because well, it's not returning any thing.
Try to return a promise like the example below however I am not sure what the dispatch argument supposed to do so I have removed it and hopefully this'll help:
export const testFunction = () => {
return new Promise((resolve, reject) => {
otherFunction().then(( response ) => {
//do something...
resolve(response);
}).catch(( error ) => {
//do something...
reject(error);
});
});
}
when you are trying to return a promise to use it in another file, you must use the following syntax:
const testFunction = () => {
return new Promise((resolve, reject) => {
if (error) {
return reject(error); // this is the value sent to .catch(error)
}
return resolve(valueToReturn); // this is the value sent to .then(result)
});
}
This is how you create a promise to use where you want, if it has an error it will sent to catch block, otherwise you should see the console.log(result) value.
And in your external file you can use the syntax that you are using, try it in that way to see the console.log value.
Here is a link to see more information: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise
I am confused with the use of promise, specifically of its way of data manipulation (passing values from block to block) and exception handling (bubbling up the error). I am trying to learn a right way to use promise and to handle error, something like
Error: A caught error.
at promiseTwo()
at promiseOne()
at subprocess()
at mainprocess()
Here are my two attempts in implementing them:
Attempt 1: Clumsy, deeply nested, and errors are uncaught.
var subprocess = () => {
return new Promise((resolve, reject) => {
promiseOne().then(data1 => {
// Some code with data1, throw some error
promiseTwo().then(data2 => {
// Some code with data1n2, throw some error
promiseThree().then(data3 => {
// Data manipulation with data1, data2, and data3
return resolve(<...>)
}).catch(err3 => { throw err3 })
}.catch(err2n3 => { throw err2n3 }) // >>> ERR: Cannot get err3.
}.catch(err1n2n3 => { return reject(err1n2n3) }) // >>> ERR: Cannot get err3 or err2.
}
}
return new Promise((resolve, reject) => {
subprocess().then(data => {
// TODO
}).catch(allErr => { return reject(allErr) }
}
Attempt 2: Unable to use data from previous promise block.
var subprocess = () => {
return new Promise((resolve, reject) => {
promiseOne()
.then(data1 => {
// Some code with data1, throw some error
return promiseTwo()
})
.then(data2 => {
// Some code with data1n2, throw some error
// >>> ERR: Cannot get data1
return promiseThree()
})
.then(data3 => {
// Data manipulation with data1, data2, and data3
// >>> ERR: Cannot get data1 and data2
return resolve(<...>)
})
.catch(err1n2n3 => {
return reject(err1n2n3)
})
}
}
return new Promise((resolve, reject) => {
subprocess().then(data => {
// Some code, throw some error
}).catch(allErr => { return reject(allErr) }
}
Note: Some of the promise block (i.e. promiseOne, promiseTwo, etc.) are pre-defined so I do not have control over what data they will return. I am sure there are more errors in the attempts (e.g. if returning a function is a right way to do it).
Please help. Thanks.
for this kind of situation, you can combine promises and async-await together.
From the question, it seems we have three promises and one function that executes and handle them.
You can try something like this -
const subProcess = () => {
return new Promise((resolve, reject) => {
// Using IIFE ( You shouldn't put async keyword on promise callbac )
(async () => {
// Use of try catch to handle the errors
try {
await promiseOne()
await promiseTwo()
await promiseThree()
// Additional code if need after them
} catch(err){
// Handle error ( all three promise error will be transferred here )
}
})()
})
}
The above code waits for the promises to execute one by one and also catch error from all three promises if any.
And as #samuei mentioned, you can also use Promise.all() in this.
const subProcess = () => {
return new Promise((resolve, reject) => {
// Using IIFE ( You shouldn't put async keyword on promise callbac )
(async () => {
// Use of try catch to handle the errors
try {
const myPromises = [promiseOne, promiseTwo, promiseThree];
const res = await Promise.all(myPromises);
// Additional code if need after them
} catch(err){
// Handle error ( all three promise error will be transferred here )
}
})()
})
}
And if you don't want to use async-await then you can do something like this as well
const subProcess = () => {
return new Promise((resolve, reject) => {
const myPromises = [];
const myPromises = [promiseOne, promiseTwo, promiseThree];
Promise.all(myPromises)
.then(res => {
// Handle the response
})
.catch(err => {
// Handle the error
})
})
}
It sounds like you're looking for Promise.all, which lets you set a series of promises in motion, then deal with the results when they are all resolved.
I want to return results from two database queries in one object.
function route(start, end) {
return new Promise((resolve, reject) => {
const queries = routeQuery(start, end);
var empty_obj = new Array();
for (i=0; i<queries.length; i++) {
query(queries[i], (err, res) => {
if (err) {
reject('query error', err);
console.log(err);
return;
} else {
empty_obj.push(res.rows);
}});
}
console.log(empty_obj);
resolve({coords: empty_obj});
});
}
This is my code right now, the queries are working fine but for some reason, pushing each result into an empty array does not work. When I console log that empty object, it stays empty. The goal is to resolve the promise with the generated object containing the two query results. I'm using node-postgres for the queries.
Output of res is an object:
{
command: 'SELECT',
rowCount: 18,
oid: null,
rows: [
{ ...
I suggest you turn your query function into a Promise so that you can use Promise.all:
// turn the callback-style asynchronous function into a `Promise`
function queryAsPromise(arg) {
return new Promise((resolve, reject) => {
query(arg, (err, res) => {
if (err) {
console.error(err);
reject(err);
return;
}
resolve(res);
});
});
}
Then, you could do the following in your route function:
function route(start, end) {
const queries = routeQuery(start, end);
// use `Promise.all` to resolve with
// an array of results from queries
return Promise.all(
queries.map(query => queryAsPromise(query))
)
// use `Array.reduce` w/ destructing assignment
// to combine results from queries into a single array
.then(results => results.reduce(
(acc, item) => [...acc, ...item.rows],
[]
))
// return an object with the `coords` property
// that contains the final array
.then(coords => {
return { coords };
});
}
route(1, 10)
.then(result => {
// { coords: [...] }
})
.catch(error => {
// handle errors appropriately
console.error(error);
});
References:
Promise.all - MDN
Array.reduce - MDN
Destructing assignment - MDN
Hope this helps.
The issue you currently face is due to the fact that:
resolve({coords: empty_obj});
Is not inside the callback. So the promise resolves before the query callbacks are called and the rows are pushed to empty_obj. You could move this into the query callback in the following manner:
empty_obj.push(res.rows); // already present
if (empty_obj.length == queries.length) resolve({coords: empty_obj});
This would resolve the promises when all rows are pushed, but leaves you with another issue. Callbacks might not be called in order. Meaning that the resulting order might not match the queries order.
The easiest way to solve this issue is to convert each individual callback to a promise. Then use Promise.all to wait until all promises are resolved. The resulting array will have the data in the same order.
function route(start, end)
const toPromise = queryText => new Promise((resolve, reject) => {
query(queryText, (error, response) => error ? reject(error) : resolve(response));
});
return Promise.all(routeQuery(start, end).map(toPromise))
.then(responses => ({coords: responses.map(response => response.rows)}))
.catch(error => {
console.error(error);
throw error;
});
}
My script is throwing the following error when returning the result of the assync firestore set function:
You must return a Promise in your transaction()-callback.
According to firebase documentation about transactions, set function return a transaction itself.
Here a simplified copy of my code.
var myDoc = {
field1: "v1"
};
var docRef = db
.collection("docs")
.doc("1");
return db
.runTransaction(t => {
return t
.set(docRef, chat, {merge:false}); //has i understand, this should return a transaction object but the error says otherwise.
})
.then( doc => {
response.send();
})
.catch(err => {
...;
})
I am still new to Nodejs and not very familiar with chaining assyncs methods, so i must be doing some obvious error here.
Haven't used firestore transaction but I have used firebase transactions. You can try following
return db
.runTransaction(t => {
return t.set(docRef, chat, {merge:false})
.then(data => {
return Promise.resolve('transaction complete');
})
.then( doc => {
response.send();
})
.catch(err => {
...;
})
and the method that encloses your whole code must be returning promise as you have written return db.runTransaction(t => {....})
so if that is not needed then use
var transaction = db.runTransaction(t => {...});
Just do this:
return db
.runTransaction(t => {
t.set(docRef, chat, {merge:false});
return Promise.resolve(); // Add this line.
})
.then( doc => {
response.send();
})
.catch(err => {
...;
})
If the t.set() failed, it won't go to the resolve() anyway.
I'm trying to promisify node-adodb using bluebirdjs.
I've tried this:
import Promise from 'bluebird'
import ADODB from 'node-adodb'
const db = ADODB.open(`...`)
const dbQuery = db.query(`...`)
const dbQueryOn = Promise.promisify(dbQuery.on, { context: dbQuery })
dbQueryOn('done').then(data => {
console.log('data =', data)
}).catch(err => {
console.log('err =', err)
})
The data is returned, but it comes via the .catch() not the .then() method.
How do I get node-adodb working with promises..?
I'm not familiar with node-adodb, but from its documentation it seems it uses a non-conventional way of returning errors and results (using event-like emitters).
Bluebird's promisify requires the regular Node.js callback convention (first argument represents errors, second argument represents the "result" value), so you can't use it in this situation.
But you can wrap it yourself:
const db = ADODB.open(`...`);
const runQuery = query => {
return new Promise((resolve, reject) => {
db.query(query)
.on('done', resolve)
.on('fail', reject);
});
}
// Usage:
runQuery(`...`).then(data => {
console.log('data =', data)
}).catch(err => {
console.log('err =', err)
})