Skip to content

BUG | useAsyncState | execute() should directly return the resolved data of the current promise after execution #5111

@l246804

Description

@l246804

Describe the bug

In the source code, state.value is returned every time after execute() is executed. However, if execute() is called multiple consecutive times, the .then() of the non-last calls should obtain the resolved data of the current execution.

Here's a concrete bug example:

// Reactive object to store results
const dicts = reactive({})

// Mock async function: returns different data with different delays based on code
const getDict = async (code) => {
  if (code === 1) {
    await promiseTimeout(100) // Fastest: resolves after 100ms
    return { value: 'a', label: 'A' }
  } else if (code === 2) {
    await promiseTimeout(2000) // Slowest: resolves after 2000ms
    return { value: 'b', label: 'B' }
  } else {
    await promiseTimeout(1000) // Medium: resolves after 1000ms
    return { value: 'c', label: 'C' }
  }
}

const { isLoading, executeImmediate } = useAsyncState(getDict, undefined,{ immediate: false })

// 1st execution: should resolve to { value: 'a', ... }
executeImmediate(1).then((v) => {
  // ACTUAL: v is undefined (throws error when accessing v.value)
  // EXPECTED: v should be { value: 'a', label: 'A' }
  dicts[v.value] = v
})

// 2nd execution: should resolve to { value: 'b', ... }
executeImmediate(2).then((v) => {
  // ACTUAL: v is { value: 'c', label: 'C' } (overwritten by 3rd execution)
  // EXPECTED: v should be { value: 'b', label: 'B' }
  dicts[v.value] = v
})

// 3rd execution: should resolve to { value: 'c', ... }
executeImmediate(3).then((v) => {
  // ACTUAL: v is { value: 'c', label: 'C' } (only this one works)
  // EXPECTED: v should be { value: 'c', label: 'C' }
  dicts[v.value] = v
})

Reproduction

https://playground.vueuse.org/?vueuse=14.0.0#eNqlVlGP4jYQ/iujvAASm8DuPSE4HbdHpavu2tPtVqrU9ME4k8W7iR3ZkwBC/PeOE8KGFdCtKvHgeMbffJ9nPMMumBdFWJUYTIKpk1YVBA6pLCAT+mkWB+Ti4GOsVV4YS7CDwppcOXxUOZqShlA6nLutlg8kCGEPKduh94kR2RJJY7HXOW1RSFLVqyP7sT3WUQQ/W5tZPqMkIAOO+DwfcmXGPLQ02hEkSpKD2RGrv9sPDhDfjXwB4elAWmq2Gj1hPyqtdnwuTdGiZgRBAtaKVt09zMTWwVI4TMBokCbBNuIT0hcOyjEb7L43DmD2EXaxBlBpswOz2QzGg2YTQKyFojf31R+PRgNgpr8IR+jIs3Mmq9CBSAktsD1npf58w5svrRJZiRPoid6Qs7LEjNfzHuy92x4wc3hK4fY6hdvRgcNDZtbnOHiHiySWHRKfT0n8i+4m6HdMVJmf0305puzEvG9jxpp/bYZ2oNw3IxKln4aAG5Ql4dc851hNVc5O67R/yCiXr04wVRqTIUO0ByaQCi/oWFdjDtGg1hXlVqbMklaCL9TTJIVh6Pm95dEfD0Jaoe73q9fiYfT5/eMf828TqFjEKyHo08qatQO01lhY80EQUqJzLBKqsA7IBGuIxZ8/FvePiy8e5MBuyQm5Wjr1O/rrAPQ3X1HFV9oqvtXJexX7irik+PYdii/nGfqmQru2iojFL7dwZzus3qv9XMVe1X4S5bp2z/iS9rv/q11nW6AVexmNsDb2xb1X8rkHc0nyNGraPjd5/iDMi4zZ8xfANFEVyEw4x4MgzXADiu3uRnLD5Df7XDpS6bb9XN8wEHKiVodFPTj8g/Y4hyV3y/ahTmDXebaw30+XFqKjX8239mka/r5WwWBRi9auplGHdTDkkcVNIVVP4bMzmudaffNxIE1eqAzt74XPK481Bm8g40Bk3At/rffIljhs9+UK5cuZ/We38Xtx8IPLAm2FcXC0kbDcXhrz4uE33PD6aMxNUmbsfcX40xdaXXuN22duCUy741ez/VrPVL65R7fY8PtwrShPtO6QtX8c8Ii9vyL9le5d+KHtrHyLzcy+yUXx5h4bwylIO+/dSlhMGt4rosJNokgmmhF4vqrKhhop0kUenR74NP4QjsIR59NRpLgDbvhER3D378R/APfuF6CPQvf/AAqU+dY=

System Info

System:
    OS: Linux 6.14 Ubuntu 25.04 25.04 (Plucky Puffin)
    CPU: (8) x64 11th Gen Intel(R) Core(TM) i5-1135G7 @ 2.40GHz
    Memory: 4.90 GB / 14.78 GB
    Container: Yes
    Shell: 5.2.37 - /bin/bash
  Binaries:
    Node: 22.17.1 - /run/user/1000/fnm_multishells/317555_1761111904305/bin/node
    npm: 10.9.2 - /run/user/1000/fnm_multishells/317555_1761111904305/bin/npm
  Browsers:
    Firefox: 144.0
    Firefox Developer Edition: 144.0
  npmPackages:
    @vueuse/core: ^14.0.0 => 14.0.0 
    vue: ^3.5.22 => 3.5.22

Used Package Manager

pnpm

Validations

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions