During the development process, I have encountered many asynchronous problems, which are solved in a very stupid way. I have encountered them again today. I decided to use promise for a try. After looking at it for a while, I found that the simple use of promise is very simple, so I feel like sharing it with Xiaobai. Don’t say much nonsense, just code it.
$(function() {
new Promise(function(resolve, reject) {
$.ajax({
type: 'post',
url: 'http://route.showapi.com/341-1',
dataType: 'jsonp',
data: {
// "showapi_timestamp": formatterDateTime(),
"showapi_ Appid ": 3893 ', // you need to change it to your own appid
"showapi_ Sign ":'ec12dec8a6a7446af6d88ba7f2bf79c '// you need to change it to the secret of your application,
},
Jsonp: 'jsonpcallback' // this method name is very important and cannot be changed
success: function(res) {
The resolve (RES) // console variable cannot be used in the lower version of IE
},
error: function(XmlHttpRequest, textStatus, errorThrown) {
reject();
},
});
}).then((data) => {
console.log (data) // this is the execution after the request is successful
}).catch((res) => {
console.log ('failed! )
});
})
AJAX is also applicable to fetch. I hope it will be helpful to novices.