Wrap your Promise inside a function or it will start to do its job right away. Plus, you can pass parameters to the function:
var some_function = function(username, password)
{
return new Promise(function(resolve, reject)
{
/*stuff using username, password*/
if ( /* everything turned out fine */ )
{
resolve("Stuff worked!");
}
else
{
reject(Error("It broke"));
}
});
}
Then, use it:
some_module.some_function(username, password).then(function(uid)
{
// stuff
})
?
ES6:
const some_function = (username, password) =>
{
return new Promise((resolve, reject) =>
{
/*stuff using username, password*/
if ( /* everything turned out fine */ )
{
resolve("Stuff worked!");
}
else
{
reject(Error("It broke"));
}
});
};
Use:
some_module.some_function(username, password).then(uid =>
{
// stuff
});
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…