OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

How do I convert an existing callback API to promises?

  • Thread starter Thread starter Benjamin Gruenbaum
  • Start date Start date
B

Benjamin Gruenbaum

Guest
I want to work with promises but I have a callback API in a format like:

###1. DOM load or other one time event:

Code:
window.onload; // set to callback
...
window.onload = function() {

};

###2. Plain callback:

Code:
function request(onChangeHandler) {
    ...
}
request(function() {
    // change happened
    ...
});

###3. Node style callback ("nodeback"):

Code:
function getStuff(dat, callback) {
    ...
}
getStuff("dataParam", function(err, data) {
    ...
})

###4. A whole library with node style callbacks:

Code:
API;
API.one(function(err, data) {
    API.two(function(err, data2) {
        API.three(function(err, data3) {
            ...
        });
    });
});

How do I work with the APIs in promises, how do I "promisify" them?

<p>I want to work with promises but I have a callback API in a format like:</p>
<p>###1. DOM load or other one time event:</p>
<pre><code>window.onload; // set to callback
...
window.onload = function() {

};
</code></pre>
<p>###2. Plain callback:</p>
<pre><code>function request(onChangeHandler) {
...
}
request(function() {
// change happened
...
});
</code></pre>
<p>###3. Node style callback ("nodeback"):</p>
<pre><code>function getStuff(dat, callback) {
...
}
getStuff("dataParam", function(err, data) {
...
})
</code></pre>
<p>###4. A whole library with node style callbacks:</p>
<pre><code>API;
API.one(function(err, data) {
API.two(function(err, data2) {
API.three(function(err, data3) {
...
});
});
});
</code></pre>
<p>How do I work with the APIs in promises, how do I "promisify" them?</p>
 

Latest posts

Top