A
A
Alexander Knyazev2017-01-23 15:02:30
JavaScript
Alexander Knyazev, 2017-01-23 15:02:30

How, using needle, sending a get request to return a value from the callback?

This code for sending a get request outputs a large object with a bunch of properties. I need to get what I returned with "return"

function findMaxID() {
    var a = needle.get(URL, function(err, res){
        if (err) throw err;
        return 222; 
    });
    console.log(a);
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ilya Shatokhin, 2017-01-23
@iShatokhin

No way with return. Only callbacks/promises. This is an asynchronous request.

function findMaxID(cb) {
    needle.get(URL, cb);
}

findMaxID(function(err, res){
    if (err) throw err;
    console.log(res); // -- данные доступны здесь
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question