Promise. How to pass the argument data in the future .then()?

Trying to understand the chips ES6. Can't understand the Promise.

For example, the function - decided to make some "beautiful" analog to Fetch JsonP for training. Can't understand how using Promise to pass the argument in the next then.

Here is the piece of code - function to retrieve json from jsonp (probably, curve):
//...
getJsonp(url) {
 return new Promise(() => {
 const d = document, s = d.createElement("script");
 window.callbackFunc = function(data) {
 delete window.callbackFunc;
 return r;
};
 s.async = true;
 s.src = url;
d.body.appendChild(s);
});
};
//...


The idea is that in the end I want to be able to call it like this:
this.getJsonp("/url/file.jsonp?callback=callbackFunc").then(data => {
console.log(data);
});


Please help me to understand
August 19th 19 at 22:40
2 answers
August 19th 19 at 22:42
Solution
Promise passes 2 parameters.
new Promise(function(resolve, reject) { ... });
so when the time comes to return via a call resolve(value)

Description here
A function object with two arguments resolve and reject. The first argument is the successful implementation of promises, the second rejects it. We can call these functions at the end of our operation.

Read, but not understood until the end. That is in itself the functions to do so:
window.callbackFunc = function(data) {
 delete window.callbackFunc;
 return resolve(data);
};

Right you know? - emmy_Hilll commented on August 19th 19 at 22:45
: promise to not return it caused 2 functions, one sends the result to then (resolve) in the second catch (reject) ,see my example in the answer - reyna.Greenholt commented on August 19th 19 at 22:48
Yes. Just enough window.callbackFunc = function(data) {
delete window.callbackFunc;
resolve(data);
}; - cleve_Murray commented on August 19th 19 at 22:51
now I understand, thank you. Difficult novelty lost long ago perceived... - emmy_Hilll commented on August 19th 19 at 22:54
: promises powerful thing, very helpful when you start to die all debugit ))) - reyna.Greenholt commented on August 19th 19 at 22:57
, , thank you. Didn't think so simple... Always looking for something the trick, especially something new. Especially after using a callback and some custom implementations, then... - emmy_Hilll commented on August 19th 19 at 23:00
August 19th 19 at 22:44
Solution
your promis marks vnikuda
const myPromise = () => {
 return new Promise((resolve, reject)=>{
 let key = setTimeout(reject, 30000, 'timeout Promise');
 // Some action asynchronously for the result of the promis
removeTimeout(key);
 resolve(data); // <-- here we sent the data to then
});
};

Use:
myPromise()
.then(response=>{
 console.log('data: ', response);// here flew our data .... 
})
 .catch(console.error);

Find more questions by tags JavaScript