Monday 20 March 2017

How are async callbacks implemented?

I've seen a lot of examples that uses callbacks for asyn events like reading, dowloading, fetching etc, but I can't figure it out how it's working under the hood..Here's what I've tryed to do, but I can't get it working..function fetch(url, cb) { var data setTimeout(() => { data=`{"hello":"man"}` }, 3000) cb(null, data) } cb = (err, data) => { if (err) return err else console.log(data) }; fetch("http://url.com", cb); As you can see, I'm trying to implement a fake fetch function where the data is delayed. I've been passing a callback, but still got undefined.. Anyway how I could get this working without promises or anything else, only vanilla js?

Submitted March 20, 2017 at 12:01PM by shark1337

No comments:

Post a Comment