Skip to content

Instantly share code, notes, and snippets.

@sibsfinx
Last active June 1, 2017 14:39
Show Gist options
  • Save sibsfinx/3f59bb28564c49e9ca21ac72b68a5e6b to your computer and use it in GitHub Desktop.
Save sibsfinx/3f59bb28564c49e9ca21ac72b68a5e6b to your computer and use it in GitHub Desktop.
Can't make promise return value
// need to execute a chain of promises (using then())
// and make/call a function that will return a value (not a promise object)
p = (fn) => {
return new Promise((resolve, reject) => {
setTimeout( () => {
resolve(fn.call());
}, 1000);
});
};
log = () => {
console.log('step');
return 1;
}
prom = null;
prom = p(log).then((res) => {
return p(log);
}).then((res) => {
return p(log);
}).then((res) => {
return 1;
});
async function aspr() {
r = await prom.then((v) => {
window.result = v;
});
console.log(window.result);
return window.result;
}
aspr();
@andretges
Copy link

p = (fn) => {
  return new Promise((resolve, reject) => {
    setTimeout( () => {
      resolve(fn.call());
    }, 1000);
  });
};

log = () => {
  console.log('step');
  return 1;
}

x = async () => { 
  await p(log).then((res) => {
    window.result = res;
  });
}

x();
window.result;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment