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

.
Menu