How To Add Properties To A Promise Asynchronously?
In this question : How to add a custom property or method to a promise? there are simple solutions regarding how to 'add properties' to a promise function when the properties are k
Solution 1:
As Bergi noted, just because we can do this does not mean we should. In fact, i highly recommend against it.
Regardless, it is possible using a builder and a proxy:
Assume the properties are asynchronously defined as follows:
var properties_to_append = {
load : function(path){
returnthis.then((view_loader)=>{ console.log("loading " + path); return view_loader.load(path)}) // define `view_loader.load()` to the view_loader promise
},
generate : function(options){
returnthis.then((compiler)=>{ return compiler.generate(options) })
},
}
var promise_properties = Promise.resolve(properties_to_append);
Then utilizing the AsyncPropertyPromise
class defined further down the following works as expected:
var async_property_promise = newAsyncPropertyPromise(require("clientside-view-loader"), promise_properties);
async_property_promise // works
.load("clientside-view-modal-login_signup") // works
.generate() // works
.then((modal)=>{
document.body.appendChild(modal);
modal.show("login");
})
AsyncPropertyPromise:
var unknown_properties_deferment_handler = {
return_defined_target_value : function(target, prop){
var value = target[prop];
var bound_value = typeof value == 'function' ? value.bind(target) : value; // bind functions to target, as they would expectreturn bound_value; // return the requested name or parameters
},
get: function(target, prop) {
if(prop in target){
returnthis.return_defined_target_value(target, prop); // if the requested method or parameter is in the target object, just return it
} else {
return target.promise_to_attempt_to_get_async_property(prop);
}
}
};
classAsyncPropertyPromise{
constructor(original_promise, promise_properties) {
this.original_promise = original_promise;
this.promise_properties = promise_properties;
var proxied_self = new Proxy(this, unknown_properties_deferment_handler);
return proxied_self;
}
then(...args) {
returnthis.original_promise.then(...args);
}
catch(...args){
returnthis.original_promise.catch(...args);
}
promise_to_attempt_to_get_async_property(property){
/*
1. return a function - NOTE - this assumes that any property not statically defiend is a function
2. make that function resolve with an AsnycPropertyPromise that
a. returns the value of the property (method) if it exists
b. throws error if it does not
*/return function(...args){ // 1var raw_response_promise = this.promise_properties // 2
.then((loaded_properties)=>{
if(!(property in loaded_properties)) throw"property not defined"; // 2.avar value = loaded_properties[property];
var bound_value = value.bind(this); // bind to original_promisereturn bound_value(...args); // evaluate and return response while passing orig arguments; see `spread` https://stackoverflow.com/a/31035825/3068233
});
var async_proxied_response_promise = this._wrap_a_promise(raw_response_promise);
return async_proxied_response_promise;
}
}
_wrap_a_promise(raw_promise){
return new this.constructor(raw_promise, this.promise_properties);
}
}
Post a Comment for "How To Add Properties To A Promise Asynchronously?"