object Promise on return - node.js

Whenever I'm trying to generate random keys from crypto or nanoid (new library) it just returns [object Promise]
async function createCode(length){
if(!length){
return nanoid;
} else {
return nanoid(length);
}
}
// another one for example
async function createCode(){
return await crypto.randomBytes(64).toString('hex');
}

An async function returns a promise by default. Please call await createCode() in another async function or use createCode().then()

All async function return a promise. Always.
So, the caller of an async function has to either use await themselves (from within another async function) or use .then() to get the value out of the promise.
It doesn't look to me like either of your functions benefit from being async. return await someFunc() when someFunc() returns a promise can be done with just return someFunc() just the same. The await is not necessary at all.
FYI, crypto.randomBytes() uses a callback if you want the asynchronous version. If you don't pass it a callback, then it's just a plain synchronous function. Unless you've done something to make a promisified version of that library, it doesn't return a promise.
So, you can just use this:
// another one for example
function createCode(){
return crypto.randomBytes(64).toString('hex');
}
Which you can just call as a regular function:
let randomVal = createCode();
console.log(randomVal);
If you want the asynchronous version and want to use it with promises, then you'd have to promisify it:
// another one for example
function createCode(){
return new Promise((resolve, reject) => {
crypto.randomBytes(64, function(err, val) {
if (err) return reject(err);
resolve(val.toString('hex'));
});
});
}
Then, you can call it:
createCode().then(val => {
console.log(val);
}).catch(err => {
console.log(err);
});

Related

why is async keyword added in a node function while node is already asynchronous ? why we need to tell this is an asysnchronous function?

I want to know actual meaning of async keyword usage before function name in node js.
Node.js is asynchronous in the idea that code does not need to run in the order it was called. However, you have to tell Node to do that for you.
Defining a function as async means that the function will return a Promise that resolves to whatever the return of the function is:
// This function returns a `Promise` that resolves to `true`
const func = async () => {
return true;
}
Importantly, defining a function as async also allows the use of the await keyword:
const func = async () => {
const result = await fetch("someUrl");
return await result.toJSON();
}
async/await is just syntactic sugar for traditional Promises:
// This is the same function as above
const func = () => {
return fetch("someUrl")
.then(result => result.toJSON())
}
An async function will ALWAYS return a promise. If it returns a non-promise, the return value will be wrapped in Promise.resolve. If it throws, it will wrap the exception in Promise.reject.
So the async keyword is a contract that guarantees the return value of the function to be asynchronus.
And this is required for the await keyword, because you can only await if you return a promise.

How to await in a chain?

This is a Azure Function httpTrigger in node.js. How to wait for f1() finishes then calls f2()?
model.exports = main(param) // async main plus await f1(param) don't wait either.
{
f1(param)
.then(
good => f2(good, callback), // execution jumps into f2 without waiting for f1 finishes
bad => {...}
);
}
async function f1(param)
{
try
{
// await NOT wait here,
// but waits in Express
await googlelib.verifyIdToken(something, (err, ok) =>
{
if (err) { return Promise.reject("Invalid"); }
else { return Promise.resolve("OK"); }
});
}
catch (e) { return Promise.reject("catch"); }
}
If googlelib.verifyIdToken() supports promises as a built-in part of its interface (as long as you do not pass it a callback), then you can change your code to this:
function f1(param) {
return googlelib.verifyIdToken(something);
}
And, you will just directly return the promise that googlelib.verifyIdToken() provides, allowing the caller to use either await or .then() on the result that f1(...) returns.
If googlelib.verifyIdToken() does not support promises as a built-in part of its interface (when you do not pass it a callback), then you can "promisify" it yourself:
const {promisify} = require('util');
// create promisified interface .verifyIdTokenP
googlelib.verifyIdTokenP = promisify(googlelib.verifyIdToken);
// use that promisified interface
function f1(param) {
return googlelib.verifyIdTokenP(something);
}
This process of manually promisifying a function works for any asynchronous API that supports the "nodejs asynchronous calling convention" where the function accepts a callback as the last argument and that callback will be called with two arguments as callback(err, value). If the asynchronous API works differently than this, then you can't use the util.promisify() function for it and would have to manually wrap it in your own promise.

Is it OK to pass an async function to setImmediate in order to call an arbitrary function asynchronously?

I want to call a given function asynchronously. The wrapper function tryCallAsync is one way of doing this. This approach works. However, it requires that the callback for setImmediate to be an async function. This seems wrong, as the callback is returning a Promise that is not used. Is it wrong to pass an async function to setImmediate for this purpose?
async function tryCallAsync(fn, ...args) {
return new Promise((r, j) => {
setImmediate(async () => {
try {
r(await fn(...args));
}
catch (e) {
j(e);
}
})
})
}
// Using tryCallAsync
let resolveAsync = tryCallAsync(()=>{
return new Promise((r,j)=>{
setImmediate(()=>r('resolveAsync'));
});
})
resolveAsync.then((resolve)=>console.log(resolve));
let resolve = tryCallAsync(()=>{
return 'resolve';
});
resolve.then((resolve)=>console.log(resolve));
NB: https://www.youtube.com/watch?v=e3Nh350b6S4
Yes, it's wrong, for multiple reasons:
setImmediate doesn't handle the returned promise, especially it doesn't deal with errors1
Don't put business logic in asynchronous (non-promise) callbacks when using promises. Settle a promise from there, nothing else.
1: And even while your particular callback never rejects the returned promise due to the try/catch, it still feels wrong
Your function should be written as
async function tryCallAsync(fn, ...args) {
await new Promise(resolve => {
setImmediate(resolve);
});
return fn(...args);
}
This approach doesn't waste a Promise, however, still, it's not as performant as the conventional way of doing this.
function tryCallAsync(fn, ...args) {
return new Promise((r, j) => {
setImmediate(() => {
(async function () {
return await fn(...args);
})().then(r).catch(j);
});
});
}

Node.js MongoDb find function ignores wait

I need the callback function of find from Node.js mongodb 3.1.6 to be triggered before the return statement of an async function, however the return statement is called before the callback function even-though there is a wait in front.
async function(myId) {
const myObject = MyObject()
await collection.find({where: {id: myId}}, async (err, results) => {
if (err) {
logger.error('error');
}
myObject.add(results);
});
return myObject
}
I have seen some examples where instead of find(query, callback) the pattern find(query).toArray() was used. But this doesn't run at all in my case. We use Node.js mongodb 3.1.6 with loopback-connector-mongodb maybe this is related to the problem.
If mongo doesn't provide a promise-answering function, then promisify this one yourself. Neither that promise-creating wrapper nor the anonymous callback it uses should be declared async, but the caller should....
function findById(collection, myId) {
return new Promise((resolve, reject) => {
collection.find({where: {id: myId}}, (err, results) => {
(err)? reject(err): resolve(results);
});
});
}
// now callers can use the async await pattern...
async someFunction() {
try {
let myId = // ...
let collection = // ...
let results = await findById(collection, myId);
// do something with results
} catch (err) {
// error
}
}
The key idea is that collection.find with the callback isn't eligible for await, because it doesn't return a promise. The anonymous callback function you pass to it isn't an async function... it does its work right away, as soon as find calls it back. So we build a promise around mongo, then use the new async/await syntax with that promise.

node.js middleware making code synchronous

I am trying to make res.locals.info available on every single page.
I'm trying to do this by middleware but I'm getting an error.
Apparently res.locals.info is not ready yet when the page render, thus I get an error info is not defined. How do I solve this?
app.use(function(req,res,next){
async function getInfo(user) {
let result = await info.search(user);
setInfo(result);
}
function setInfo(result){
res.locals.info= result;
}
getInfo(req.user);
return next();
})
search():
module.exports.search= function (user) {
var query=`SELECT count(*) as Info from dbo.InfoUsers WHERE user= '${user}' ;`
return new Promise((resolve, reject) => {
sequelize
.query(`${query}`, {model: InformationUser})
.then((info) => {
resolve(info);
})
})
};
You were calling next() before your getInfo() function had done its work, thus res.locals.info had not yet been set when you were trying to use it.
An async function returns a promise. It does NOT block until the await is done. Instead, it returns a promise immediately. You will need to use await or .then() on getInfo() so you know when it's actually done.
If info.search() returns a promise that resolves to the desired result, then you could do this:
app.use(function(req,res,next){
// this returns a promise that resolves when it's actually done
async function getInfo(user) {
let result = await info.search(user);
setInfo(result);
}
function setInfo(result){
res.locals.info= result;
}
// use .then() to know when getInfo() is done
// use .catch() to handle errors from getInfo()
getInfo(req.user).then(result => next()).catch(next);
});
And, you can remove the deferred anti-pattern from your search function and fix the error handling (which is a common issue when you use the anti-pattern). There is no need to wrap an existing promise in another promise.:
module.exports.search = function (user) {
var query=`SELECT count(*) as Info from dbo.InfoUsers WHERE user= '${user}' ;`
// return promise directly so caller can use .then() or await on it
return sequelize.query(`${query}`, {model: InformationUser});
};

Resources