O
O
olezhenka2017-03-28 08:50:26
Node.js
olezhenka, 2017-03-28 08:50:26

How to make a value in a function global node.js?

the res value is returned in the function, how can this value be used outside the function?
ce3c2b83971f4b44aba8adb3cf95edcc.PNG

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
rustler2000, 2017-03-28
@rustler2000

etozh callback - so will never work.
at you at first create will be caused, then console.log. but whether the callback between them will be called - this is written with a pitchfork on the water.
```
ttttt.create({...}), function (err, res) {
if (err) throw err;
console log(res);
return res;
});
```

D
de1m, 2017-03-28
@de1m

Through async should work. You only need nodejs 7.7.4, it works for me on this version.

var output;
var genNum = async function () {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            return resolve(Math.random())
        }, 1000)
    })
}

output = await gennum();
console.log(output);

I used it here , you can see it there.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question