How to encapsulate an async with export

The original meaning of

clipboard.png


clipboard.png
is that I want to encapsulate an asynchronous
and then call this asynchronous
outside to return both successful and error methods

.
Oct.15,2021

**ES6 Promise,:http://es6.ruanyifeng.com/-sharpdocs/promise**


export function test (url,data,type) {
   return new Promise((resolve,reject)=>{
        $.ajax({
            url,
            data,
            type,
            datatype:'jsonp',
            success:function(data){resolve(data);},
            error:function(res){reject(res);}
        })
     })
   }


I think you need to understand the concepts of asynchronism and Synchronize!
I think you can change it like this and pass in two callback functions

.
let test(data,success,error) {
    return $.ajax({
        url:'www.baidu.com',
        data:data,
        type:'get',
        datatype:'jsonp',
        success:success,
        error:error
    })
}

Modification of calls

export default{
    data () {
        retrun {
            aa: null
        }
    },
    methods: {
        init () {
            var _this = this
            var data = {}
            var success = function (data) {
                _this.aa = data
            }
            var error = function (res) {
                console.log(res)
            }
            test(data,success,error)
        }
    },
    mounted () {
        this.init()
    }
}

if you don't want to put aa in data and define a var aa in success, you can also execute it

.
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1ea88f3-47cc9.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1ea88f3-47cc9.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?