1

I want to use Promises to execute my tasks in synchronous order and for this, I have 3 following functions:

  1. init(): My promise function to orchestre this;
  2. saveUser(): function to save the user object into database;
  3. main function that is responsible to run it in synchronous order.

It's working fine without pass the 'user' variable. I would like to know, how can I pass this 'user' variable into my saveUser.bind() to be run into my Promise and be saved by userSave() function?

// promise function
function init(fn) {
  return new Promise((resolve, reject) => {
    fn((err, result) => {
      if (err) reject(err);
      console.log('the saveUser function ran inside my promise...');
      resolve(result);
    });
  });
}

// saveUser function, need receive the user variable and show in console log
function saveUser(callback, user) {
  console.log('1. the user will be save...', user);
  setTimeout(() => {
    console.log('2. the user was saved!');
    return callback(null, true);
  }, 1000);
}

// main function that I need to pass the user variable into saveUser() function
let user = {name: 'Rick'};
init(saveUser.bind(x => x)).
  then(() => console.log('3. Promise finished with success!')).
  catch((err) => console.log('3. Promise was rejected!', err));
Biruel Rick
  • 776
  • 1
  • 8
  • 17
  • what are you trying to achieve? what's your goal? because I have a gut feeling that you're overcomplicating your code... – lukaleli Nov 06 '17 at 14:31
  • Passing callback first is weird, the node convention is to have it as the last parameter. Have a look at [How do I convert an existing callback API to promises?](https://stackoverflow.com/q/22519784/1048572) for many `init`-like functions then – Bergi Nov 06 '17 at 14:53

3 Answers3

1

The first param of bind is this.
The bind code will look like this. saveUser.bind(x=>x, x=>x, user)

// promise function
function init(fn) {
  return new Promise((resolve, reject) => {
    fn(resolve,reject);
  });
}

// saveUser function, need receive the user variable and show in console log
function saveUser(callback, user,resolve,reject) {
  console.log('1. the user will be save...', user);
  setTimeout(() => {
    console.log('2. the user was saved!');
    resolve(callback(null, true));
  }, 1000);
}

// main function that I need to pass the user variable into saveUser() function
let user = {name: 'Rick'};
init(saveUser.bind(this, x=>x, user)).
  then(() => console.log('3. Promise finished with success!')).
  catch((err) => console.log('3. Promise was rejected!', err));
artgb
  • 3,177
  • 6
  • 19
  • 36
1

I'm not sure if exactly how to organize the thing but below there's your snippet working

// promise function
function init(fn, thisArg, ...params) {
  return new Promise((resolve, reject) => {
    fn.apply(thisArg, [(err, res) => {
      console.log('the saveUser function ran inside my promise...');
    }, ...params]).then((data) => {
      resolve(data);
    }).catch((err) => {
      reject(err);
    });
  });
}

function saveUser(callback, user) {
  return new Promise((resolve, reject) => {
    console.log('1. saving...');
    setTimeout(() => {
      console.log('2. the user was saved! user name is ', user.name);
      resolve(callback(null, user));
      // reject('reason');
    }, 200);
  });
}

// main function that I need to pass the user variable into saveUser() function
let user = {name: 'Rick'};
init(saveUser, null, user).
  then(() => console.log('3. Promise finished with success!')).
  catch((err) => console.log('3. Promise was rejected!', err));

you could even save the first resolver/rejecter somewhere or pass them to fn when calling it if you don't want to create a new Promise for that.

FrontTheMachine
  • 526
  • 3
  • 14
  • Thanks FeMachine, but I need that promise return me the resolve() function to execute the following .then(). This works but resolve() not return. The console.log '3. Promise finished with success!' not appears – Biruel Rick Nov 06 '17 at 14:57
0
init(cb => saveUser(cb, user))

Or

Change saveUser(callback, user) to saveUser(user, callback) and then

init(saveUser.bind(this, user))
Ankur
  • 33,367
  • 2
  • 46
  • 72