Skip to content

Events called synchronously after write operation #125

@brentahiti

Description

@brentahiti
  • Operating System version: OS X 10.12.6
  • Node version: 6.9.1
  • Library version: 5.4.3
  • Firebase Product: database

Basically, child_added/child_changed events (listened on locally) are called synchronously after .set() or .update() for example.

I'm not sure if it's made by design, but I find this behavior counterintuitive. I would expect code right after a set to be run before any event callback gets called.

'use strict';

const firebase = require('firebase-admin');
const firebaseApp = firebase.initializeApp({
    credential: firebase.credential.cert('path/to/credentials.json'),
    databaseURL: '<app>.firebaseio.com'
});

const ref = firebaseApp.database().ref();


ref.child('test').on('child_changed', (snap) => {
    console.log('child_changed called on', snap.key);
});

const testPush = () => {
    console.log('pushing in test')
    const pushId = ref.child('test').push({ attr: 'initial' }).key;
    console.log('after pushing in test')
    return pushId;
};

const testChange = (id) => {
    console.log('set in', id)
    ref.child('test').child(id).set({ attr: 'changed' });
    console.log('after set in', id)
};

const id = testPush();
testChange(id);

The above code prints

pushing in test
after pushing in test
set in -KxyYoYapMEfNd-i7rvB
child_changed called on -KxyYoYapMEfNd-i7rvB
after set in -KxyYoYapMEfNd-i7rvB

My expectations are

pushing in test
after pushing in test
set in -KxyYoYapMEfNd-i7rvB
after set in -KxyYoYapMEfNd-i7rvB
child_changed called on -KxyYoYapMEfNd-i7rvB

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions