简体   繁体   中英

Why do I need to use a callback is this case

I'm trying to work out the Coinbase Pro node.js API.

When running this:

const buyParams = {
  price: '100.00', // USD
  size: '1', // BTC
  product_id: 'BTC-USD',
};
authedClient.buy(buyParams, callback);

I have the following error callback is not defined

So I'm trying to understand how does callback work exactly.

What is the callback here exactly? If I understand well I need to define the callback which is a function but what would be the purpose of this function? I this because I need to wait for the answer from authedClient method? Or this something else?

.buy method will call provided callback when it finishes.
You probably want to do something depending on the result.
That's why you need that callback.

Usually callbacks are used when a task takes some time and for one of two reasons: to "give" you the value that took so long to prodce, or to notify you about an event, usually "I'm done".

In NodeJs it's a common pattern to use callbacks like this function(error, value){...} where the function is either called with an error, if one occurred callback(err); or the value callback(null, result);

In your case, you need to read in the API-Doc how and when the callback is called. What arguments are provided, etc.

I'm asking why do I need to define it.

Because the author of the buy() method thaught that every user of this function would want to be noified when this task is done, and would want to recieve whatever value this callback provides, so there must be an error, if you don't provide a proper callback-function. That's why he throws an Error.

You should read API carefully, but in this situation the second parameter of .buy is a function you passed to it to execute after .buy runs.

const buyParams = {
price: '100.00', // USD
size: '1', // BTC
product_id: 'BTC-USD',
};
const cb = ()=> {
 console.log('Buy finished!');
}
authedClient.buy(buyParams, cb);

you should read API document for parameters of cb. but in simple way it's some thing like up code! and after buy finished cb called and log the 'Buy Finished';

feel free to ask more questions.

Typically, you use callback functions in order to be called when the functions finish his work. But why callbacks and not simply catch the return result of the function? Because of synchronous events.

Check out here and the following code to understand better the problem of async functions and why javascript programmers solve it by using callback functions

 function syncFunc() { return "finished!"; } function asyncFunc() { setTimeout(() => { return "finished!"; },1); } function asyncFuncWithCallback(callback) { setTimeout(() => { callback("finished"); },1); } console.log(`syncFunc: ${syncFunc()}`); console.log(`asyncFunc: ${asyncFunc()}`); asyncFuncWithCallback((value) => { console.log(`asyncFuncWithCallback: ${value}`); });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM