Added a promises-based lock around flush.

Added code to _get, _put, _del, _batch and _close to wait for any executing flush before continuing.
Modified the flush function to wait for itself as well.