最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Make fetch call really synchronous - Stack Overflow

programmeradmin5浏览0评论

Yes, I want to do it pletely synchronous. I know that it will pletely stop my one and only thread, but I really need that, because I use some SDK which I don't want to change and in this SDK you need to pass a function that will be called and that will change some value in there like that:

function onNonce(stuff) {
    const url = 'fancy url to change stuff';

    // await also doesn't work

    // const response = await fetch(url);
    // const resp_json = await response.json();
    // return resp_json.token;

    // await also doesn't work

    const req = new XMLHttpRequest();
    req.open("GET", url, false); // <-- pletely sync and deprecated
    req.send();

    if(req.readyState === 4 && req.status === 200) {
        return req.response.token;
    }
}

And this is how my func is called:

function SDK(result) {
    //
    // SOME FANCY CODE
    //

    var the_value_to_change;

    the_value_to_change = onNonce('some stuff');
    console.log("async");

    //
    // SOME FANCY CODE that uses this the_value_to_change
    //
}

If I use await then my func returns Promise instead of the token, and if I use open with true (async), then I get undefined. The variant with false (pletely sync) is deprecated, so I want to do the same stuff with fetch API.

// EDIT //

So, how can I do the execution of onNonce function (fetch and response.json()) pletely synchronous?

Yes, I want to do it pletely synchronous. I know that it will pletely stop my one and only thread, but I really need that, because I use some SDK which I don't want to change and in this SDK you need to pass a function that will be called and that will change some value in there like that:

function onNonce(stuff) {
    const url = 'fancy url to change stuff';

    // await also doesn't work

    // const response = await fetch(url);
    // const resp_json = await response.json();
    // return resp_json.token;

    // await also doesn't work

    const req = new XMLHttpRequest();
    req.open("GET", url, false); // <-- pletely sync and deprecated
    req.send();

    if(req.readyState === 4 && req.status === 200) {
        return req.response.token;
    }
}

And this is how my func is called:

function SDK(result) {
    //
    // SOME FANCY CODE
    //

    var the_value_to_change;

    the_value_to_change = onNonce('some stuff');
    console.log("async");

    //
    // SOME FANCY CODE that uses this the_value_to_change
    //
}

If I use await then my func returns Promise instead of the token, and if I use open with true (async), then I get undefined. The variant with false (pletely sync) is deprecated, so I want to do the same stuff with fetch API.

// EDIT //

So, how can I do the execution of onNonce function (fetch and response.json()) pletely synchronous?

Share Improve this question edited Oct 31, 2018 at 16:54 Ruslan Plastun asked Oct 31, 2018 at 16:48 Ruslan PlastunRuslan Plastun 2,2524 gold badges27 silver badges53 bronze badges 4
  • 3 What is your question? – Jaskier Commented Oct 31, 2018 at 16:52
  • 1 It was deprecated in the main thread due to its blocking nature. It would not make to much sense to introduce it again in the modern follow up fetch API. So no you can't do that. – t.niese Commented Oct 31, 2018 at 16:55
  • 1 There is nothing in the above that prevents the SDK from being used asynchronously. As such we cannot help you because I personally cannot just trust that it is impossible to use the SDK asynchronously just because you say so. Show us how the SDK is meant to be used and we can maybe show you how to integrate asynchronous code with the SDK. Just because you think it's impossible does not mean it's impossible for people creative enough. – slebetman Commented Mar 10, 2023 at 7:40
  • Related: How to make javascript fetch synchronous? – ggorlen Commented Aug 25, 2024 at 13:51
Add a ment  | 

1 Answer 1

Reset to default 1

You can't.

fetch does not have an option to run synchronously (for the same reason that the option to run XHR synchronously was deprecated in the first place).

Learn how to use await properly instead.

发布评论

评论列表(0)

  1. 暂无评论