0

我目前正在使用黑莓动态 SDK。

我目前正在使用 SDK 的 http 请求功能,但每次我想从 http 调用返回响应时,它总是未定义 - 我尝试承诺它返回一个值,但无济于事。

它最初使用了两个回调 - 这将正确地返回我未定义的,但如果我将它作为一个承诺,它应该不会返回我一个值。

代码

function constructGDHttpPostRequest(reqObj) {
    let PostRequest = window.plugins.GDHttpRequest.createRequest("POST", URI + reqObj.endPoint, 30, false);
    PostRequest.addRequestHeader('Content-Type', 'application/json');
    PostRequest.addHttpBody(reqObj.body);
    return SendRequest(PostRequest).then(function (httpRes) {
        console.log(httpRes);
        return httpRes;
    })
}

function SendRequest(Request) {
    return new Promise(function (resolve) {
        resolve(Request.send(sendSuccess));
    })
}

function sendSuccess(response) {
    console.log("Received valid response from the send request");
    let Response = window.plugins.GDHttpRequest.parseHttpResponse(response);
    return JSON.parse(Response.responseText);
}

我曾尝试使用与此类问题相关的一些问题,但它仍然未从承诺中返回。

提前喝彩。

4

1 回答 1

0

根据@Nikos M. 的建议,这已经完成,现在按预期工作。

我需要解析回调才能返回一个值。

我想通过一些建议使回调更清晰。

   function constructGDHttpPostRequest(reqObj) {
        let PostRequest = window.plugins.GDHttpRequest.createRequest("POST", URI + reqObj.endPoint, 30, false);
        PostRequest.addRequestHeader('Content-Type', 'application/json');
        PostRequest.addHttpBody(reqObj.body);
        return SendRequest(PostRequest).then(function (httpRes) {
            console.log(httpRes);
            return httpRes;
        })
    }

    function SendRequest(Request) {
        return new Promise(function (resolve) {
            Request.send(function (response) {
                resolve(JSON.parse(window.plugins.GDHttpRequest.parseHttpResponse(response).responseText));
            });
        })
    }
于 2018-11-26T17:01:39.500 回答