Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Promise问题顾总 #34

Open
wohaofang opened this issue Jul 31, 2020 · 0 comments
Open

Promise问题顾总 #34

wohaofang opened this issue Jul 31, 2020 · 0 comments

Comments

@wohaofang
Copy link
Owner

Promise 状态

  • pending :初始状态
  • fulfilled: 意味着操作已经完成
  • rejected:意味着操作已经失败
    一个处于 pending 态的 promise 是不可能转换成完成态(fulfilled)或拒绝态(rejected)的。 当这些状态发生的时候,会调用 promise 的 then 方法处理队列里的相关处理程序

Promise 封装 jsonp

import myJsonp from 'jsonp';
// jsonp封装
export default function jsonp(url, data, paramURL = "") {
    const param = {
        "jsonParams": JSON.stringify(data),
        'credithc-request-client-type': 'html5'
    }
    url = `${baseURL[paramURL]}${url}`

    url += (url.indexOf('?') < 0 ? '?' : '&') + params(param)
    return new Promise((resolve, reject) => {
        myJsonp(url, (err, data) => {
            if (!err) {
                resolve(data)
            } else {
                reject(err)
            }
        })
    })
}

export function params(data) {
    let url = '';
    for (var k in data) {
        let value = data[k] !== undefined ? data[k] : ''
        url += `&${k}=${encodeURIComponent(value)}`
    }
    return url ? url.substring(1) : ''
}
// api 封装
export function getSome(param, paramURL = '') { //
    console.log('start')
    return api('/hyd/services/hydUser/smsCodeForLogin', param, paramURL).finally(() => {
        console.log('end')
    })
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant