Skip to content

Android headless JS timeouts not working #33043

@marcesengel

Description

@marcesengel

Description

Currently TimingModule related operations don't work reliably in background tasks, as noted in the docs

The function passed to setTimeout does not always behave as expected. Instead the function is called only when the application is launched again. If you only need to wait, use the retry functionality.

this issue invertase/react-native-firebase#3955

Throwing a headlessJsTaskError seems the only way to make setTimeout work in the headless js task.

and this StackOverflow question.

I am developing a react-native applicaton with a headless js task to make some background work.

I need to execute some code after an amount of time, but noticed that using setTimeout execution gets suspended until the app is again in foreground.

Anyone has idea why this happens and know how to solve or workaround this?

I myself recently stumbled upon this issue as well and went into debugging it. For my findings and the solution I'll create a PR shortly.

Version

all

Output of npx react-native info

Issue is independent of build system and react-native version.

Steps to reproduce

Create a headless JS task using some TimingModule related code (for example setTimeout) and execute it while the app is in background (for example using a FCM data-only message).

Snack, code example, screenshot, or link to a repository

Example headless JS task handler:

module.exports = async (taskData) => {
  console.log('started headless JS task')
  await new Promise(resolve => setTimeout(resolve, 1))
  console.log('resolved timeout')
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions