Skip to content

Instantly share code, notes, and snippets.

@vituchon
Last active April 21, 2023 13:21
Show Gist options
  • Select an option

  • Save vituchon/a72a9af8aaaadb670d09c70914f1ac34 to your computer and use it in GitHub Desktop.

Select an option

Save vituchon/a72a9af8aaaadb670d09c70914f1ac34 to your computer and use it in GitHub Desktop.
Construyendo deferreds a manopla
function deferBuilder<T>() {
type resolveType = (value: T) => void;
interface resolveInterface { // no funca con interface :S
(value: T): void;
}
type rejectType = (reason?: any) => void
var resolve: resolveType;
var reject: rejectType
const promise = new Promise(function (_resolve: resolveType, _reject: rejectType) {
console.log("newPromise")
resolve = _resolve
reject = _reject
})
console.log("after newPromise")
return {
promise: promise,
resolve: resolve,
reject: reject
}
}
var a = deferBuilder<number>();
a.promise.then((valor) => {
console.log("valor:", valor)
})
console.log("before resolve")
a.resolve(54)
console.log("after resolve")
@fernandezpablo85
Copy link

fernandezpablo85 commented Apr 17, 2023 via email

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