I want to use Promises to execute my tasks in synchronous order and for this, I have 3 following functions:
- init(): My promise function to orchestre this;
- saveUser(): function to save the user object into database;
- 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));