user19283043
user19283043

Reputation: 357

how to convert callback to promise

I'm trying to learn about what the promise is and how to convert callback to promise. While I'm converting my code to promise I got very confused about the ref. I would very appreciate it if you show me how to convert this code as a simple example.

database.ref('/users').on("child_added").then(function(snap){
  var subData = snap.val();

  database.ref('/subs/' + subData.subid + '/pri/' + snap.key).once("value").then(function(userSnap) {
    var userData = userSnap.val();

    subData.name = userData.name;
    subData.age = userData.age;

    database.ref('/subs/' + subData.subid).once("value",function(subDSnap) {
      var subDData = subDSnap.val();
      subData.type = subDData.type;
      database_m.ref('/users/' + snap.key).set(subData);
    });
  });  
});

Upvotes: 1

Views: 325

Answers (2)

Jacob
Jacob

Reputation: 78850

A Promise is not a replacement for every type of callback; rather it's an abstraction around one particular task that will either succeed once or fail once. The code you're converting looks more like an EventEmitter, where an event can occur multiple times, so replacing .on('child_added', ...) with a Promise implementation is not a good fit.

However, later on, you have a .once(...) call. That's a bit closer to a Promise in that it will only complete once. So if you really wanted to convert that, here's what it could look like:

function get(database, url) {
  return new Promise(function (resolve, reject) {
    database
      .ref(url)
      .once('value', resolve)
      .once('error', reject);
  });
}

database.ref('/users').on("child_added", function(snap) {
  var subData = snap.val();

  get(database, '/subs/' + subData.subid + '/pri/' + snap.key)
    .then(function(userSnap) {
      var userData = userSnap.val();

      subData.name = userData.name;
      subData.age = userData.age;

      return get(database, '/subs/' + subData.subid);
    })
    .then(function(subDSnap) {
      var subDData = subDSnap.val();
      subData.type = subDData.type;
      database_m.ref('/users/' + snap.key).set(subData);
    })
    .catch(function (err) {
      // handle errors
    });
  });  
});

Upvotes: 1

Fab
Fab

Reputation: 1865

I am not sure I understand the question and if your "on" is returning a promise or just listening, but in your code you have nested '.then', which is not the common way to deal with promises and I am not sure that's what you wanted to achieve here.

You could do (assuming that the on function returns a promise, which I doubt)

 database.ref('/users').on("child_added")
.then(function(snap){/* do something with the first part of your snap function*/})
.then (results => {/* do something else, such as your second ref call*/})
.catch(error => {/* manage the error*/})

To learn about promises, there are many examples online but what I really liked is the promise tutorial at google, with this nice snippet that explains it

var promise = new Promise(function(resolve, reject) {
  // do a thing, possibly async, then…

  if (/* everything turned out fine */) {
    resolve("Stuff worked!");
  }
  else {
    reject(Error("It broke"));
  }
});

then, once you have the function that returns this promise, you can start doing

.then(...)
.then(...)
.then(...)
.catch(...)

Upvotes: 0

Related Questions