jspsych / datapipe

Send data from your behavioral experiments to the OSF. Born-open data as a service.
https://pipe.jspsych.org
MIT License
25 stars 1 forks source link

How to make datapipe wait for async functions that dynamically set experiment_id? #100

Open xallysie opened 1 month ago

xallysie commented 1 month ago

People usually have a predetermined experiment_id for each script/experiment.

Instead, I'm using a script that dynamically chooses from an array of experiment_ids, and then saves the data to one of these osf components. To choose which experiment_id to use, I need to first run an async function that reads data from an existing firebase database.

Unfortunately, I can't find a way to make this resolve before datapipe runs. I've tried pushing the async function to a separate node first, or adding an "on_start: async function()" property to the datapipe save_data node, but I keep getting the same error that the experiment_id is missing.

Here's an example of something I've tried:

const save_data_osf = {
    on_start: async function(){
        // wait for recruitment wave to be set
        osf_DataComponent_Name = await setRecruitmentWave();
    },
    type: jsPsychPipe,
    action: "save",
    experiment_id: osf_DataComponent_Name,
    filename: filename,
    data_string: ()=>jsPsych.data.get().csv()
};

If datapipe or jsPsych generally doesn't play well with async functions, I'll try something else, but putting this question here if there's an obvious solution!

jodeleeuw commented 1 month ago

Hi @xallysie,

You can use the call-function plugin which supports asynchronous function calls. I'd add it to the timeline right before the save_data_osf trial, and store the ID in a global variable.