problem description
original question: how to solve the problem that the promise of es6 cannot execute reslove multiple times?
the current problem: how to overwrite the previous problem if the constructor mode is called multiple times?
the environmental background of the problems and what methods you have tried
original description:
I encapsulate an ajax method and use promise, but I cannot execute resolve when a page calls this ajax method for the second time.
I try to find out that the state of promise can only be changed once, but I create a new promise every time I execute the method.
I think it may be related to my design pattern, if the design pattern is not changed. How to solve this?
now describes:
I encapsulated an ajax method using constructor mode?
but when the page is called multiple times, it will cause the previous ajax method to be overwritten.
how to modify the least amount of code to change the ajax method multiple times at the same time.
related codes
/ / Please paste the code text below (do not replace the code with pictures)
var my = function() {
// beforeAjax, ajax, afterAjax
* @param {string} url [description]
* @param {object} data []
* @param {Function} callback []
* @param {object} setting [ajax]
this.ajax = function(url, data, callback, setting) {
// beforeAjax, ajax, afterAjax
new Promise((resolve, reject) => {
beforeAjax(() => {
resolve(0);
});
}).then(number => {
return new Promise(resolve, reject) {
ajax(() => {
resolve();
});
}
}).then(() => {
afterAjax(() => {
console.log(url);
callback();
})
});
}
return this;
}
window.$my = new my();
expected results
when calling, $my.ajax (url, data, callback, setting);
if the url passed in is a.com, b.com, c.com;
prints out c.com, c.com, c.com