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

javascript - How to write async fetch() without async? - Stack Overflow

programmeradmin3浏览0评论

I have a line of code that uses await fetch(). I'm using some script injecting that invokes eval( "await fetch ...etc..." ), but the problem is that await doesn't go when it's called from eval().

So, the aim is to rewrite just this one line without using await. How to do that?

( await fetch( ff_url, ff_params ) )
                                  .json()
                                  .then( data => window.items_list = data.items );

Upd: This code is used with AutoHotkey. There's no need to split the line into two (neither split in JavaScript or AutoHotkey) to check if fetch is already done because in my case (AutoHotkey + JS) I think it's simpler to periodically verify if window.items_list has a value or is still undefined :D

P.S. Thanks for answers, it works fine!

I have a line of code that uses await fetch(). I'm using some script injecting that invokes eval( "await fetch ...etc..." ), but the problem is that await doesn't go when it's called from eval().

So, the aim is to rewrite just this one line without using await. How to do that?

( await fetch( ff_url, ff_params ) )
                                  .json()
                                  .then( data => window.items_list = data.items );

Upd: This code is used with AutoHotkey. There's no need to split the line into two (neither split in JavaScript or AutoHotkey) to check if fetch is already done because in my case (AutoHotkey + JS) I think it's simpler to periodically verify if window.items_list has a value or is still undefined :D

P.S. Thanks for answers, it works fine!

Share Improve this question edited Aug 9, 2019 at 10:16 Redbraid asked Aug 9, 2019 at 7:58 RedbraidRedbraid 911 gold badge2 silver badges8 bronze badges 4
  • Can you post more context of the code? Do you want the eval mand to wait until the fetch is done before going onto the next line? – CertainPerformance Commented Aug 9, 2019 at 8:01
  • Responses from this question may help you :) – Ulysse BN Commented Aug 9, 2019 at 8:08
  • @CertainPerformance, updated the post: this is injection made via AutoHotkey. So I'm sure splitting the line into two evals - before and after fetch done - is not needed, because anyway (until await is not supported) I'll need to verify the value of window.items_list manually, like this: loop { if( PageInst.Evaluate( "typeof window.items_list === 'undefined'" ).Value == false ) { break } Sleep, 150 } – Redbraid Commented Aug 9, 2019 at 8:40
  • @UlysseBN Thanks I'll read that :D For now, Promises and operator => are the most difficult topics for me, in addition to callbacks :) – Redbraid Commented Aug 9, 2019 at 9:21
Add a ment  | 

1 Answer 1

Reset to default 9

If the problem is simply that you can't mark the method async then standard promise syntax would do:

fetch(ff_url, ff_params)
    .then(x => x.json())
    .then(data => window.myvariable = data.items);

Of course you should still ensure that caller calls this asynchronously and handles the result appropriately (unless fire and forget is desirable).

发布评论

评论列表(0)

  1. 暂无评论